Microsoft Small Basic
RSS

Navigation





Quick Search
»
Advanced Search »

PoweredBy

Solution to Project Euler Problem 9

RSS
Modified on 2011/08/16 13:39 by 94.179.31.172 Categorized as Samples
Find the only Pythagorean triplet, {a,b,c}, for which a + b + c = 1000

If a right angled triangle sides are a, b and c, with the hypotenuse being c (Pythagorean triplet), then:

a2 + b2 = c2 (Pythagoras)

Also we have the condition that:

a + b + c = 1000

Now some simple algebra to combine the equations

c = 1000-(a+b) starting with the second, rearrange for c

a2 + b2 = (1000-(a+b))2 insert c into Pythagoras' equation

a2 + b2 = 1000000 - 2000*(a+b) + (a+b)2 multiply out RHS

a2 + b2 = 1000000 - 2000*(a+b) + a2 + 2*a*b + b2 multiply out RHS again

0 = 1000000 - 2000*(a+b) + 2*a*b remove a2 + b2 from both sides

2000*(a+b) - 2*a*b = 1000000 rearrange to put all the a's on the LHS

1000*(a+b) - a*b = 500000 divide everything by 2 to simplify

a(1000-b) + 1000*b = 500000 factor all the a terms together

a(1000-b) = 500000 - 1000*b rearrange to put only the a's on the LHS

a = (500000-1000*b) / (1000-b) divide though to get an equation for a in therms of b that satisfies original equations

Now all we have to do is try lots of b up to 1000, and find a case where a is an integer, we assume there is only one

For b = 1 To 1000
  a = (500000-1000*b)/(1000-b)
  ' Test that a in an integer - it is an integer if it is equal to itself rounded down, or Floor of itself
  If (Math.Floor(a) = a) Then
    Goto found
  EndIf
EndFor

found:
'Find c
c = 1000-a-b
'Write a, b and c
TextWindow.WriteLine("a= "+a)
TextWindow.WriteLine("b= "+b)
TextWindow.WriteLine("c= "+c)
'Test and output results of test
TextWindow.WriteLine("a+b+c= "+(a+b+c))
TextWindow.WriteLine("a*a + b*b = "+(a*a+b*b))
TextWindow.WriteLine("c*c = "+(c*c))

a = 375 b = 200 c = 425 weight loss pills weight loss pills

ScrewTurn Wiki version 3.0.5.600. Some of the icons created by FamFamFam.