Move Project Euler under puzzles.
This commit is contained in:
46
puzzles/project_euler/p009_SpecialPythagoreanTriplet.py
Normal file
46
puzzles/project_euler/p009_SpecialPythagoreanTriplet.py
Normal file
@@ -0,0 +1,46 @@
|
||||
#
|
||||
#Special Pythagorean triplet
|
||||
#Problem 9
|
||||
#
|
||||
#A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
|
||||
#
|
||||
#a^2 + b^2 = c^2
|
||||
#For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
|
||||
#
|
||||
#There exists exactly one Pythagorean triplet for which a + b + c = 1000.
|
||||
#Find the product abc.
|
||||
#
|
||||
#Solution:
|
||||
#
|
||||
|
||||
# a^2+b^2=c^2=(1000-a-b)^2= 1000^2-2000(a+b)+(a+b)^2=
|
||||
# = 1000^2-2000(a+b)+a^2+2ab+b^2 =>
|
||||
# => 0=1000^2-2000(a+b)+2ab =>
|
||||
# => 500000=1000(a+b)-ab
|
||||
# => 500=a+b - ab/1000
|
||||
# => ab is dividable to 1000=2*2*2*5*5*5
|
||||
# => a dividable at least to 5 and b to 2
|
||||
#
|
||||
#for a in range(5,1000,5):
|
||||
# for b in range(2,1000-a,2):
|
||||
# i+=1
|
||||
# if (a**2+b**2) == (1000-a-b)**2:
|
||||
# print( a, b, 1000-a-b, '->', a**2, '+', b**2, '=', a**2+b**2 )
|
||||
# print( ' while (1000-a-b)**2 =', (1000-a-b)**2 )
|
||||
# print( 'product =', a*b*(1000-a-b) )
|
||||
# print( 'product =', (a+b)*(5*10**5+10**3) - (a+b)**2 - 5*10**8 )
|
||||
#
|
||||
# a better solution
|
||||
# => 500000=1000(a+b)-ab
|
||||
# => (500000-1000a)/(1000-a)=b
|
||||
# => 1000(500-a)/(1000-a)=b
|
||||
|
||||
for a in range(1,500):
|
||||
r=1000*(500-a)
|
||||
q=1000-a
|
||||
b=r//q
|
||||
if (a**2+b**2) == (1000-a-b)**2:
|
||||
print( a, b, 1000-a-b, '->', a**2, '+', b**2, '=', a**2+b**2 )
|
||||
print( ' while (1000-a-b)**2 =', (1000-a-b)**2 )
|
||||
print( 'product =', a*b*(1000-a-b) )
|
||||
print( 'product =', (a+b)*(5*(10**5)+10**3) - (a+b)**2 - 5*(10**8) )
|
||||
Reference in New Issue
Block a user