Euler: Integer right triangles

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}

p是三边长度均为整数的直角三角形的周长,当p=120时,存在三种直角三角形。分别是:
{20,48,52}, {24,45,51}, {30,40,50}

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

p的范围限定在1000以内,那么p为多少时,具有最多种情况的直角三角形?

思路:从p整数中取出三个数x,y,z,使x>y>z且x+y+z=p。并且要满足y+z>x。


def isRight(x,y,z):
    if not(x>=y and y >= z):
        print "should be x>y>z!", str(x),str(y),str(z)
        return False
    if x*x == y*y + z*z:
        return True
    return False


def getnum(p):
    if p<3 or p>1000:
        print 'Wrong p'
        return
    count = 0       
    for x in range(p/3, p/2):
        for y in range((p-x)/2, (p-x)):
            if x<y:
                break
            z = p-x-y
            if y<z:
                continue
            #print "x,y,z=",str(x),str(y),str(z)
            if isRight(x,y,z):
                print "x,y,z=",str(x),str(y),str(z)
                count += 1

    print "count for p is ", str(count)
    return p,count

def main():
    #getnum(120)
    maxcount = 1
    maxp = 1
    for i in range(3, 1001):
        tmp_p,tmp_count = getnum(i)
        if tmp_count > maxcount:
            maxcount = tmp_count
            maxp = tmp_p

    print maxp, maxcount

if __name__ == "__main__":
    main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值