码迷,mamicode.com
首页 > 其他好文 > 详细

EularProject 39:给周长判断构成直角三角形个数

时间:2015-07-30 13:31:14      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

华电北风吹
天津大学认知计算与应用重点实验室
完成日期:2015/7/30

Integer right triangles
Problem 39
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?
Answer:
840
Completed on Thu, 30 Jul 2015, 04:51
Go to the thread for problem 39 in the forum.
利用的性质
b+c=l?a
c?b=a2l?a
a<=b<c
a+b>c
其中第二个性质整除a可以大大减少运算时间

__author__ = ‘zhengyi‘

def IsRightTriangle(abc):
    a2=pow(abc[0],2)
    b2=pow(abc[1],2)
    c2=pow(abc[2],2)
    temp=a2+b2-c2
    if temp==0:
        return 1
    else:
        if temp<0:
            return 0
        else:
            return -1

def Count(perimeter):
    count=0
    for a in range(1,perimeter//3):
        if pow(a,2)%(perimeter-a)!=0 or pow(a,2)//(perimeter-a)>=a:
            continue
        for b in range(max(perimeter//2-a,a),perimeter//2):
            temp=IsRightTriangle([a,b,perimeter-a-b])
            if temp==-1:
                break
            else:
                count+=temp
    return count

count=0
p=0
for i in range(1,1001):
    temp=Count(i)
    if temp>count:
        p=i
        count=temp

print(p)

版权声明:本文为博主原创文章,未经博主允许不得转载。

EularProject 39:给周长判断构成直角三角形个数

标签:

原文地址:http://blog.csdn.net/zhangzhengyi03539/article/details/47148169

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!