Skip to content

Instantly share code, notes, and snippets.

@goulu
Last active December 17, 2016 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goulu/21c67590c5fa96d1c7b95e3c425c90f1 to your computer and use it in GitHub Desktop.
Save goulu/21c67590c5fa96d1c7b95e3c425c90f1 to your computer and use it in GitHub Desktop.
An inefficient way to generate Pythagorean triples
import itertools, math
def triples():
""" generates Pythagorean triples sorted by z,y,x with x<y<z
"""
for z in itertools.count(5):
for y in range(z-1,3,-1):
x=math.sqrt(z*z-y*y)
if x<y and abs(x-round(x))<1e-12:
yield (int(x),y,z)
#Euler Problem 9
for (a,b,c) in triples():
if a+b+c==1000:
print(a,b,c,a*b*c)
break
@goulu
Copy link
Author

goulu commented Dec 17, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment