Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中生成原始勾股三元组列表_Python_Function_Math - Fatal编程技术网

在Python中生成原始勾股三元组列表

在Python中生成原始勾股三元组列表,python,function,math,Python,Function,Math,[改进一个封闭式问题]我看到很多问题都提出了非常类似的问题,但我还没有找到一个包含欧几里德公式的充分答案。我想尝试创建一个公式,生成一个列表,列出所有毕达哥拉斯三元组,直到给定的数字,比如k。毕达哥拉斯三元组是(x,y,z),其中x^2+y^2=z^2。它应该满足以下条件,即x尝试以下代码: def pythagore_triplets(n=20): res=[] maxn=int(n*(2**0.5))+1 # max int whose square may be the

[改进一个封闭式问题]我看到很多问题都提出了非常类似的问题,但我还没有找到一个包含欧几里德公式的充分答案。我想尝试创建一个公式,生成一个列表,列出所有毕达哥拉斯三元组,直到给定的数字,比如k。毕达哥拉斯三元组是(x,y,z),其中x^2+y^2=z^2。它应该满足以下条件,即x尝试以下代码:

def pythagore_triplets(n=20):
    res=[] 
    maxn=int(n*(2**0.5))+1 # max int whose square may be the sum of two squares
    squares=[x*x for x in range(maxn+1)] # calculate all the squares once
    reverse_squares=dict([(squares[i],i) for i in range(maxn+1)]) # x*x=>x
    for x in range(1,n):
        x2 = squares[x]
        for y in range(x,n+1):
            y2 = squares[y]
            z = reverse_squares.get(x2+y2)
            if z != None:
                res.append([x,y,z])
    return res

print(pythagore_triplets())

您可以将其编写为一个生成器,它将生成三倍的z
from math import gcd

def genTriples(k):
    n,m = 1,2
    while m*m+1<k:                  # while z<k (for largest m producing z)
        if n>=m: n,m = m%2,m+1      # n reached m, advance m, reset n
        z = m*m+n*n                 # compute z 
        if z >= k: n=m;continue     # skip remaining n when z >= k
        if gcd(n,m) == 1:           # trigger on coprimes
            yield m*m-n*n,2*m*n,z   # return x,y,z triple
        n += 2                      # advance n, odds with evens

您重复地追加到单个列表,那么为什么希望它返回3个元组呢?函数是什么?(只是在谷歌上搜索一下)那是math.gcd()函数吗?@JeffUK我试着添加三个参数,但该函数无法实现这一点。所以我试了一下。没有说代码在附近perfect@ThomasKimber是的,从数学模块开始。它找到两个数字之间的最大公约数错误在
result上。append(n*m+n*n)
应该是
result。append(m*m+n*n)
。另外,您应该将结果构建为元组:
result.append((m*m-n*n,2*m*n,m*m+n*n))
而不是附加3个单独的值。很抱歉,这还不够,因为我正在尝试使用欧几里德公式来尽可能提高代码的效率。谢谢你的回答。
from math import gcd

def genTriples(k):
    n,m = 1,2
    while m*m+1<k:                  # while z<k (for largest m producing z)
        if n>=m: n,m = m%2,m+1      # n reached m, advance m, reset n
        z = m*m+n*n                 # compute z 
        if z >= k: n=m;continue     # skip remaining n when z >= k
        if gcd(n,m) == 1:           # trigger on coprimes
            yield m*m-n*n,2*m*n,z   # return x,y,z triple
        n += 2                      # advance n, odds with evens
for x,y,z in genTriples(100): print(x,y,z)

3 4 5
5 12 13
15 8 17
7 24 25
21 20 29
9 40 41
35 12 37
11 60 61
45 28 53
33 56 65
13 84 85
63 16 65
55 48 73
39 80 89
77 36 85
65 72 97