Wednesday, 4 December 2013

Project Euler Problem 9 - Solved

Q) A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Solution:

Python: One Liner:

[(c*b*(1000-c-b)) for c in range(334,500)for b in range(250,500)if(1000-c-b)**2+b**2==c**2]


 Description:
 ===================================================
limits are decided based on following:
1) a+b+c = 1000
2) a**2 + b**2 = c**2
3) a + b > c
- a/b can't be 0 or 1 (as a<c,b<c also if a/b = 1, a+b can not be greater than c)
- c < 500
- a+b > 500
4) a<b<c
- a < 250
- b > 250 (as (a+ b) > 500)
- c > 333
- b < 500 (lesser than max of c)

No comments:

Post a Comment