计算Euler-bri

2024-04-27 05:05:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试编写代码,使用原始毕达哥拉斯三元组生成a,b,c小于1000的所有euler块。 我知道总共有十个,但我找不到一个方法来计算它们。如何使用c<;1000编写代码以获取所有10个euler砖块。任何帮助都将不胜感激!!你知道吗


Tags: 方法代码lt三元组euler砖块
1条回答
网友
1楼 · 发布于 2024-04-27 05:05:16

我想,如果你想要所有的欧拉砖,you can not use that formula.

Euler found at least two parametric solutions to the problem, but neither gives all solutions.

但是你可以这样做:

import math
i = 1
j = 1000
for a in range(i, j):
    for b in range(i, j):
        for c in range(i, j):
            d = math.sqrt(a**2 + b**2)
            e = math.sqrt(a**2 + c**2)
            f = math.sqrt(b**2 + c**2)
            if d.is_integer() and e.is_integer() and f.is_integer():
                print("a={} b={} c={}".format(a, b, c))

对其进行了优化,使其运行速度大大提高:

import math
i = 1
j = 1000
for a in range(i, j):
    a_squared = a**2
    for b in range(a, j):
        b_squared = b**2
        d = math.sqrt(a_squared + b_squared)
        if not d.is_integer():
            continue
        for c in range(b, j):
            c_squared = c**2
            e = math.sqrt(a_squared + c_squared)
            if not e.is_integer():
                continue
            f = math.sqrt(b_squared + c_squared)
            if not f.is_integer():
                continue
            print("a={} b={} c={}".format(a, b, c))

打印内容:

a=44 b=117 c=240
a=85 b=132 c=720
a=88 b=234 c=480
a=132 b=351 c=720
a=140 b=480 c=693
a=160 b=231 c=792
a=176 b=468 c=960
a=240 b=252 c=275
a=480 b=504 c=550
a=720 b=756 c=825

相关问题 更多 >