Skip to content

Instantly share code, notes, and snippets.

@evansneath
Created August 20, 2011 22:56
Show Gist options
  • Save evansneath/1159789 to your computer and use it in GitHub Desktop.
Save evansneath/1159789 to your computer and use it in GitHub Desktop.
Pythagorean Triples
def pythagorean_triples(n):
"""Finds a, b, and c such that a + b + c = n and a^2 + b^2 = c^2.
Arguments:
n: Value from which to determine pythagorean triples.
Returns:
Resulting combination of a, b, and c to satisfy the formula.
"""
a, b = 1, 1
while (a < n // 2):
while (b < n // 2):
c = n - a - b
if (a**2 + b**2 == c**2):
return a, b, c
b = b + 1
a, b = a + 1, 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment