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 xbreak
            z = p-x-y
            if ycontinue
            #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()

你可能感兴趣的:(Euler,Project)