Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A generator for Pythagorean triplets.
def pythagorean_triplets(n):
for i in xrange(1, n):
j = i+1
k = j+1
while k <= n:
while k*k < (i*i) + (j*j):
k += 1
if k*k == (i*i) + (j*j) and k <= n:
yield i, j, k
j += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment