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.

# Math Challenge

Page 1 of 1## 5 Replies - 824 Views - Last Post: 25 October 2012 - 09:08 AM

##
**Replies To:** Math Challenge

### #2

## 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

## 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

## 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.

### #5

## 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;-

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 ReadOnly Real As Decimal Public ReadOnly Imag As Decimal 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

## 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.

Page 1 of 1