题目:
(840.0, 8)
0.128999948502
别人的解决办法:
http://www.mathblog.dk/project-euler-39-perimeter-right-angle-triangle/
If 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?
import math,time
t=time.time()
count_dict=dict()
for a in xrange(1,301):
for b in xrange(a+1,501):
cpower=a**2+b**2
c=math.sqrt(cpower)
if a+b+c>1000:
break
if c**2!=cpower:
continue
if c<a or c<b:
break
#count_dict.setdefault(a+b+c,[]).append((a,b,c))
count_dict[a+b+c]=count_dict.get(a+b+c,0)+1
#print count_dict[120]
li1=count_dict.items()
#li1.sort(reverse=True,key=lambda x:len(x[1]))
li1.sort(reverse=True,key=lambda x:x[1])
print li1[0]
print time.time()-t
运行结果为:
(840.0, 8)
0.128999948502
别人的解决办法:
http://www.mathblog.dk/project-euler-39-perimeter-right-angle-triangle/