Skip to content

Instantly share code, notes, and snippets.

@Ayrx
Created July 13, 2013 08:16
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 Ayrx/5989957 to your computer and use it in GitHub Desktop.
Save Ayrx/5989957 to your computer and use it in GitHub Desktop.
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