Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
28 lines (23 sloc) 742 Bytes
pythagorean triples
a = m^2 - n^2; b = 2mn ; c = m^2 + n^2
form a Pythagorean triple.
The triple generated by Euclid's formula is primitive if and only if m
and n are coprime and exactly one of them is even. If both n and m are
odd, then a, b, and c will be even, and so the triple will not be
L = a+b+c = 2m(m+n)
from math import sqrt
from fractions import gcd
L = 1500001
Triples = [0] * L
end = int(sqrt(L>>1))
for m in xrange(1,end):
for n in xrange(1,m):
peri = m*(m+n)<<1
if gcd(m,n) != 1 or (m%2 ==1 and n%2 ==1): # m,n primitive check
for _peri in xrange(peri,L,peri):
Triples[_peri] += 1
print len([ i for i in Triples if i == 1])
Jump to Line
Something went wrong with that request. Please try again.