Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd41144
commit faead70
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |