Friday, August 03, 2007

Project Euler #39

This is my solution for Project Euler #39

its definitely brute force and im sure there is a better way to get the answer, but here we go

f p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p 1000, is the number of solutions maximised?

My Solution:-

import math, time

t = time.time()

maximum = {}

for a in xrange(1,500):
for b in xrange(1,500):
c = math.sqrt(pow(a,2)+pow(b,2))
p = a+b+c
if p == int(p) and p < 1000:
if maximum.has_key(p):
maximum[p] += 1
maximum[p] = 1

e = maximum.keys()
e.sort(cmp = lambda a,b: cmp(maximum[a],maximum[b]))
print e[-1]
print time.time()-t