Skip to content

Commit

Permalink
Solved problem 9
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvbaldawa committed Dec 30, 2011
1 parent cd41144 commit faead70
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 0008-sum-1000-digit-number.py
Expand Up @@ -46,7 +46,9 @@ def traditional():
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450"

l = []

for i in range(len(number) - 5):
l.append(int(number[i]) * int(number[i+1]) * \
int(number[i+2]) * int(number[i+3]) * int(number[i+4]))
Expand Down
29 changes: 29 additions & 0 deletions 0009-pythagorean-triplet.py
@@ -0,0 +1,29 @@
"""
Problem 9
A Pythagorean triplet is a set of three natural numbers, a b c, for which,
a2 + b2 = c2
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.
"""
import math

def is_perfect_square(n):
return int(math.sqrt(n))**2 == n

def pythagorean():
# Building a list of perfect squares
l = [x for x in range(2, 1000)]
ans = 0

for a in l:
for b in l:
c = 1000 - (a + b)
if a**2 + b**2 - c**2 == 0:
ans = a*b*c
return ans

print "Answer by traditional method:", pythagorean()

0 comments on commit faead70

Please sign in to comment.