Problem 39

completed December 8, 2011

Comments

Haha! First try! I feel awesome. I used a little mental math to cut down the number of necessary loops needed for k, m, and n: k is 1000/3 while m and n are sqrt 1000. With that little addition, this script ran in 4.8 seconds!

Resources

Wikipedia provided me with the formula to derive all Pythagorean triples, though not uniquely.

Code

triplets = []
perimeters = []
for k in range(1, 340):
	for m in range(1, 40):
		for n in range(1, 40):
			print (k, m, n)
			a = k*(m**2-n**2)
			b = k*2*m*n
			c = k*(m**2+n**2)
			if ((a+b+c) <= 1000) and ((a, b, c) not in triplets) and (a > 0):
				triplets.append((a, b, c))
				perimeters.append(a+b+c)

countlist = []
for n in range(1001):
	countlist.append((perimeters.count(n), n))
	
print max(countlist)