我该如何修复毕达哥拉斯的三元组程序?

2024-04-28 14:46:09 发布

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

import sys

def pythTrue(a,b,c):
    (A,B,C) = (a*a,b*b,c*c)
    if A + B == C or B + C == A or A + C == B:
        return True

def smallestTrip(a,b,c):
    if pythTrue(a,b,c) == True:
        if (a+b+c)%12 == 0:
            return True
        else:
            return False

def tuplePyth(n):
    list_=[]
    for x in range(1, n):
        for y in range(1, n):
            for z in range (1, n):
                if x+y+z<=n:
                    if smallestTrip(x, y, z)==False:
                        list_.append([x,y,z])
    print (list_)

tuplePyth(int(sys.argv[1]))

Pythagorean triplets are sets of 3 positive integers a, b, c satisfying the relationship a2 + b2 = c2. The smallest and best-known Pythagorean triple is (a, b, c) = (3, 4, 5). Write a program that reads a command line argument n and prints to the screen all Pythagorean triplets whose sum is less than n (i.e., a+b+c < n) and that are not multiple of the (3, 4, 5) triplet. Your program will represent triplets as 3-tuples, and should consist of three functions:

  • a function that takes in a tuple and returns a boolean indicating whether the Pythagorean relationship holds or not.
  • a function that takes in a tuple and returns a boolean indicating whether a triplet is a multiple of the smallest triplet or not.
  • a function that takes in an integer n and generates the Pythagorean triplets as specified above. The function should return a list of tuples.

The main portion of your program pythagore.py will read in the command line input, call the last function described above, and print the results one triplet per line.

我的问题是我在不同的 例如:(5,12,13)、(13,12,5)…等


Tags: orandoftheintruereturnif
3条回答

一个简单的解决方案是跟踪aleady找到的,并添加检查以避免重复。下面使用set来存储已经生成的元素,并对每个三元组中的元素进行排序,这样它们的顺序就不重要了。在

def tuplePyth(n):
    list_=[]
    seen = set()
    for x in range(1, n):
        for y in range(1, n):
            for z in range (1, n):
                if tuple(sorted((x,y,z))) not in seen:
                    if x+y+z <= n:
                        if smallestTrip(x, y, z) == False:
                            list_.append([x,y,z])
                    seen.add((x,y,z))
    print (list_)

您可以使用itertools:

 import itertools.combinations_with_replacement as cwr
 list_ = [triple for triple in cwr(range(n),3) if sum(triple)<n and not smallestTrip(triple)]

您还可以强制数字与限制顺序一致。另外,你可以通过认识到如果我们定义a是最小的数,那么它必须小于n/3(b和c都至少和a一样大,所以如果a大于n/3,那么a、b和c的和将大于n)。同样,b必须小于n/2。一旦找到了a和b的所有组合,就可以找到所有大于b小于n-a-b的c

^{pr2}$

你在日常生活中缺乏逻辑。没有什么可以强制三元组只有一个顺序:您的x和{}是可互换的,并且您保证您会同时检查这两个。在

相反,使用循环限制来强制x < y,然后确保在y或{}的值太大而不可行时停止。注意,这会去掉你的三个和的支票。在

def tuplePyth(n):
    list_=[]
    for x in range(1, n):
        for y in range(1, n):
            for z in range (1, n):
                if x+y+z<=n:
                    if smallestTrip(x, y, z)==False:
                        list_.append([x,y,z])
    print (list_)

取而代之的是:

^{pr2}$

n=100时的输出:

[[5, 12, 13], [7, 24, 25], [8, 15, 17], [9, 40, 41], [15, 36, 39], [16, 30, 34], [20, 21, 29]]

请注意,smallestTrip仍然存在问题:您的检查在逻辑上不等同于“最小三元组”。相反,检查这三个数字是否相对质数。由于Stack Overflow只允许每个帖子有一个问题,而且这个问题很容易在网上进行研究,所以我把它留给学生作为练习。:-)

相关问题 更多 >