Find Pythagorean triplet for which a + b + c = 1000

后端 未结 16 2629
离开以前
离开以前 2020-12-24 13:13

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2

For example, 32 + 4

16条回答
  •  春和景丽
    2020-12-24 13:53

    There is a quite dirty but quick solution to this problem. Given the two equations

    a*a + b*b = c*c

    a+b+c = 1000.

    You can deduce the following relation

    a = (1000*1000-2000*b)/(2000-2b)

    or after two simple math transformations, you get:

    a = 1000*(500-b) / (1000 - b)

    since a must be an natural number. Hence you can:

    for b in range(1, 500):
        if 1000*(500-b) % (1000-b) == 0:
            print b, 1000*(500-b) / (1000-b) 
    

    Got result 200 and 375.

    Good luck

提交回复
热议问题