Last active
December 15, 2015 04:39
-
-
Save caoxudong/5203688 to your computer and use it in GitHub Desktop.
Project Euler - 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.
This file contains hidden or 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
n = 1000 | |
a = 1 | |
b = 1 | |
c = 1 | |
result = 0 | |
while a < n: | |
b = a + 1 | |
while b < n: | |
c = b + 1 | |
while c < n: | |
if (a*a + b*b == c*c) and (a + b + c == n): | |
result = a * b * c | |
break | |
c = c + 1 | |
if result != 0: | |
break | |
b = b + 1 | |
if result != 0: | |
break; | |
a = a + 1 | |
print a,b,c,result |
This file contains hidden or 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
n = 1000 | |
a = 1 | |
b = 1 | |
c = 1 | |
result = 0 | |
while a < n: | |
b = a + 1 | |
while b < n: | |
c = 1000 - a - b | |
if c*c == a*a + b*b: | |
result = a * b * c | |
break | |
b = b + 1 | |
if result != 0: | |
break; | |
a = a + 1 | |
print a,b,c,result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment