Euler: Integer right triangles

来源:互联网 发布:热力计算软件 编辑:程序博客网 时间:2024/05/21 17:01

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 Falsedef 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,countdef 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, maxcountif __name__ == "__main__":    main()
0 0
原创粉丝点击