Python- 对于偶数的素数求和数

2 投票
2 回答
5053 浏览
提问于 2025-04-16 14:37

这是我教授给我的作业。我完全不知道从哪里开始,也不知道该做什么!关键是要用循环来解决这个问题,我会用循环,但这让我感到很困惑。

偶数和质数。

质数是指只有1和它自己这两个因数的数字。像2、3、5、7和11就是最初的几个质数。注意,“质数”这个概念完全是关于乘法的,跟加法没有关系。所以,如果我们开始列举偶数,可能会让人惊讶的是,它们似乎可以表示为两个质数的和(加法!)。比如:4 = 2 + 2,6 = 3 + 3,8 = 5 + 3,10 = 3 + 7,12 = 7 + 5,14 = 7 + 7,16 = 13 + 3,等等。

这总是成立吗?每个偶数都能表示为两个质数的和吗?

  1. 写一个叫做 is_prime(n) 的函数。 这个函数应该接受一个大于1的正整数 n 作为输入,并根据 n 是否是质数输出 True 或 False。你可以用一个循环来检查是否存在任何整数 d,使得 1 < d < sqrt(n) 且 d 能整除 n。建议使用 while 循环——仔细考虑循环的条件,以及何时在循环内部改变这个条件。(用布尔值来表示你的条件)。
  2. 写一个叫做 prime_sum(n) 的函数。 这个函数应该接受一个大于1的偶数 n 作为输入,并通过循环寻找两个质数 p 和 q,使得 p + q = n。提示:从 p = 3 开始。如果 (p) 和 (n-p) 都是质数,那你就完成了。如果不是,就把 p 增加2,然后再试一次。确保你不会无限循环下去!
  3. 主程序。
    • 询问用户输入一个偶数 n。不断询问,直到他们给出一个正偶数。
    • 寻找加数 p 和 q,如果找到了就打印出来,找不到就说没有。
    • 问用户是否想尝试另一个偶数,让他们继续,直到他们选择退出。

我不知道我可以编辑这个!:) 这是我目前的进展。我还没有测试它来调试,因为我想先把所有内容写下来,等错误出现时再处理,但如果你看到有什么明显的问题,请告诉我。

def is_prime(n):
    d=2
    while n>1 and d<n**0.5:
        if n%2==0:
            c=False
        d+=1
    return c

def prime_sum(n):
    p=3
    while n>1:
        q=n-p
        if q<=0:
            p+=2
            q=n-p
            is_prime(q)
        else:
            is_prime(q)
            is_prime(p)
    while True:
        print("The prime summands of", n, "are", p, "and", q)
    while False:
        print("There are no prime summands of", n)

def main():
    n=eval(input("Gimme an even number please: "))
    while True:
        n=eval(input("That is not a positive even number. Try again: "))
    #not sure how to combine yet, but I am still finishing.
    #will edit again when I have it all down.

2 个回答

-1

质数

质数是一个自然数,它只有两个不同的自然数因子:1和它自己。

A)

def is_prime(n):                 # Write a is_prime(n) function.
    if n <= 1:                   # It should accept a positive integer n>1 
        return False
    if n == 2:                   # 2 has 2 divisors 1 and itself satisfying definition
        return True
    i = 2                        # Start from 2 and check each number to the sqrt(n)
    while i < n**0.5:            # sqrt(n) can be written as n**0.5
        if n % i == 0:           # If n is divisible by i, which is not 1 or itself, 
            return False         #    return False (not prime)
        i+=1                     # Increment i by 1 and check looping condition
    return True                  # If loop breaks, return True (prime)

找出质数的方法有很多种。这是最基本的一种,唯一的优化是检查因子时只需要到n的平方根,而不是检查每一个从1到n的数字。

最基本的方法可能是:

def is_prime(n):
    if n < 2:
        return False
    for i in range(2,n):
        if n % i == 0:
            return False
    return True

B)

def prime_sum(n):
    if n % 2 or n < 1:                          # if n is odd or less than 1 return invalid
        return "invalid input"
    p = 3
    while n-p > 0:
        if is_prime(p) and is_prime(n-p):       
            return (p, n-p)                     # if both conditions are met, return prime tuple
        p+=2                                    # only check odd numbers
    return "no answer found"                    
1

别担心这个作业整体看起来很难。你可以一步一步来,因为老师已经把它分解得很清楚了。

撰写回答