Generating unique, ordered Pythagorean triplets

前端 未结 19 1308
借酒劲吻你
借酒劲吻你 2020-11-29 16:57

This is a program I wrote to calculate Pythagorean triplets. When I run the program it prints each set of triplets twice because of the if statement. Is there any way I can

19条回答
  •  难免孤独
    2020-11-29 17:30

    # To find all pythagorean triplets in a range
    import math
    n = int(input('Enter the upper range of limit'))
    for i in range(n+1):
        for j in range(1, i):
            k = math.sqrt(i*i + j*j)
            if k % 1 == 0 and k in range(n+1):
                print(i,j,int(k))
    

提交回复
热议问题