Skip to content

Instantly share code, notes, and snippets.

@abecus
Last active August 26, 2020 19:02
Show Gist options
  • Save abecus/1b08582615df42bf729154430253f5e4 to your computer and use it in GitHub Desktop.
Save abecus/1b08582615df42bf729154430253f5e4 to your computer and use it in GitHub Desktop.
def pythagoreanTriplets(n):
# calculate spf array
spf = EratosthenesSieve(2 * (n - int(sqrt(2 * n - 1))))
# looping for every values of 2*b
for b2 in range(4, 2 * (n - int(sqrt(2 * n - 1))), 2):
# calculates reduced factor of 2*b
gamma = getReducedFactorization(b2, spf)
# for findin all triplets from 2*b
for i in range(1, int(sqrt( b2 * ((b2 // 2) - 1)))//gamma + 1):
i *= gamma
sqVal = i * i
q = sqVal // b2
if q + i + (b2 // 2) > n:
break
else:
x = q + i
print((x, (b2 // 2) + i, x + (b2 // 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment