5 Replies - 797 Views - Last Post: 25 October 2012 - 09:08 AM Rate Topic: -----

#1 Warfarin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,477
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Warfarin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • 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 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

Was This Post Helpful? 2
  • +
  • -

#6 Warfarin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1