Skip to content

Instantly share code, notes, and snippets.

@goulu
Last active Dec 17, 2016
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@goulu goulu commented Dec 17, 2016

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