# Math Challenge

Page 1 of 1

## 5 Replies - 893 Views - Last Post: 25 October 2012 - 09:08 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=297000&amp;s=4734d90ce69423a710d4c00476ae4ecf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Warfarin

Reputation: 0
• Posts: 16
• Joined: 25-September 12

# Math Challenge

Posted 24 October 2012 - 08:49 AM

Ok so I was presented with a pre-calculus question and was wondering how you would code it. The problem is (3+4i)*(3-4i). The answer is 25. i = sqrt -1. For those that don't know how to solve this. You take 3*3 = 9, 3*-4i = -12i, 4i*3 = 12i, 4i*4i = 16i^2. The -12 and + 12 cancel leaving -i^2. the -i^2 and the i^2 from the 16I^2 cancel each other leaving 9 + 16 = 25.

Hopefully there is somebody smarter than me that can code this. What I need is for the 3 and the 4 to actually be variables so the user can input any number.

Thanks for trying.

Is This A Good Question/Topic? 0

## Replies To: Math Challenge

### #2 modi123_1

• Suitor #2

Reputation: 14239
• Posts: 57,131
• Joined: 12-June 08

## Re: Math Challenge

Posted 24 October 2012 - 08:54 AM

... so the challenge is to do you homework for you? That seems like an offbeat challenge.

### #3 Warfarin

Reputation: 0
• Posts: 16
• Joined: 25-September 12

## Re: Math Challenge

Posted 24 October 2012 - 08:55 AM

actually I'm the teacher. One of my students brought this from his pre-calc class.

### #4 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Math Challenge

Posted 24 October 2012 - 08:55 AM

The .Net framework already has a built in Complex number structure for you to use. Simply create a couple objects with the specified real and imaginary values and then take advantage of the operator overload for multiplcation.

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Math Challenge

Posted 24 October 2012 - 07:20 PM

Or you could (if are the teacher) could get your student(s) to write your own complex number class, which is a good exercise.
As it involves;-
Basics
• Override Base Class Functionality
• Immutability
• Thinking about scoping and accessibility.
• Defining Operators + - / *
Intermediate
• Implementing an Interface IComparable(Of T)
Since the "standard" comparison operators < <= == <> >= > are dependent on them being defined by that class. eg. If you implement IComparable(of T) then you don't get them for free.
How do they interact with other numerical types? Eg what if I want to add a Integer to a Complex Number.

For the more Adventurous / ambitious.
• Define a TryParse method.
```Public Shared Function TryParse(input As String, ByRef output As Complex) As Boolean
End Function
'''

Dim cn As Complex
If  Complex.TryParse("12 + 2i",cn) Then

```

And if you really want implement a Cast from String -> Complex defaulting to a suitable value if it isn't can be cast. For a Com

```<Immutable()>
Public Class Complex
Implemets IComparable(Of Complex)

Public Sub New(Real As Decimal, Imag As Decimal)
Me.Real = Real
Me.Imag = Imag
End Sub

Public Override Function ToString() As String
Return String.Format("{0}+i{1)",Real,Imag)
End Function

Public Shard Function Simplify(c As Complex) As Complex
' Code to do actual simplification (Normalise)
Return New Complex(c.Real,c.Imag)
End Function

Public Shared Operator +(x As Complex, y As Complex) As Complex
Return New Complex(x.Real + y.Real, x.Imag + y.Imag)
End Function

#Region "Common Complex Numbers"
Public Shared ReadOnly Zero As Complex = New Complex(0,0)
#End Region
End Class

```

This post has been edited by AdamSpeight2008: 25 October 2012 - 05:26 AM

### #6 Warfarin

Reputation: 0
• Posts: 16
• Joined: 25-September 12

## Re: Math Challenge

Posted 25 October 2012 - 09:08 AM

Thanks for the replies. This was for me as this is a basic programming class for students that have absolutely no programming experience at all.