简体   繁体   English

素数的python算法

[英]python algorithm for prime numbers

Assume the availability of a function is_prime . 假设函数is_prime可用。 Assume a variable n has been associated with positive integer. 假设变量n已与正整数关联。 Write the statements needed to find out how many prime numbers (starting with 2 and going in increasing order with successively higher primes [2,3,5,7,11,13,...]) can be added before exceeding n . 写出所需的语句,以找出在超过n之前可以添加多少个质数(以2开头,并以依次增加的质数[2,3,5,7,11,13,...]递增)。 Associate this number with the variable k . 将此数字与变量k关联。

 def main():
    n=int(input('n: '))
    k=0
    i=2
    sum=0

    while sum<=n:
        if is_prime(i):
            sum+=i
            i+=1
            k+=1
        print(k)



def is_prime(n):

    for divisor in range(2,int(n**0.5)+1):
        if n/divisor==int(n/divisor):
            return False
    return True

main()

would really appreciate some pointers. 非常感谢您提供一些指导。

I modified the code a little bit and it is working fine but the program that grades these codes says that I almost certainly should be using a + sign some where. 我对代码进行了一些修改,并且工作正常,但是对这些代码评分的程序说,我几乎可以肯定应该在某些地方使用+号。 I have no idea. 我不知道。 Modified code is: 修改后的代码是:

while sum<=n:

        if is_prime(i):

            sum+=i
            k+=1
            i+=1
    print(k)

output: 输出:

n: 10 n:10

i: 2 我:2

2 2

i: 3 我:3

5

when it should actually go upto i=5 and total =10. 当它实际上应该达到i = 5且总计= 10时。

There is actually more efficient why to solve this problem, it is Sieve of Eratosthenes . 解决这个问题的实际上是更有效的方法,那就是Eratosthenes筛 The basic idea is to generate array of numbers from 2 till n. 基本思想是生成从2到n的数字数组。 Then you iterate over this array starting from 2 and replace all numbers, which mod by i == 0 with -1 or delete them. 然后,您从2开始遍历此数组并替换所有数字,将i == 0的mod替换为-1或将其删除。

If you are getting stuck you can check implementation here. 如果您遇到困难,可以在此处检查实施。

Sieve of Eratosthenes - Finding Primes Python Eratosthenes筛-寻找Prime Python

What you certainly ought to be using is the space character around operators. 您当然应该使用的是运算符周围的空格字符。 Also, your alignment is wacky. 另外,您的对齐方式古怪。 In Python, alignment indicates structure, and your rewrite will make any interpreter complain (at least, at it's displayed here). 在Python中,对齐表示结构,您的重写将使所有解释器都抱怨(至少在此处显示)。 If a program (or a robotic TA) is reading and grading your work, and it thinks you need a + sign somewhere, then perhaps it wants to see "sum = sum + i" somewhere. 如果某个程序(或机械手TA)正在阅读和评分您的工作,并且认为您在某处需要一个+号,那么它可能希望在某处看到“ sum = sum + i”。 I'll use that. 我会用的。 So try this: 所以试试这个:

def main():
    n = int(input('n: '))
    k = 0
    i = 2
    sum = 0

    while sum <= n:
        if is_prime(i):
            sum = sum + i  # nothing wrong with "sum += i"
            k += 1
        i += 1    # put this inside "if" ==> loop forever 
    print(k)

def is_prime(m):
    for divisor in range(2, int(m**0.5) + 1):
        if m % divisor == 0:
            return False
    return True

main()
if n/divisor==int(n/divisor):

This is wrong. 错了 When you divide 2 integers you get integer as a result. 当您将2个整数相除时,将得到整数。 So 5/2 = 2. Replace it with 因此5/2 =2。将其替换为

if n%divisor == 0:

EDITED: your modified loop looks almost correct. 编辑:您修改后的循环看起来几乎正确。 If I understand your task right it should be 如果我了解您的任务正确,应该是

while sum<=n:
    if is_prime(i):
        sum+=i
        k+=1
    i+=1
print(k)

EDITED2: probably you want to stop as soon as sum + next prime is bigger then n and not add another prime value. EDITED2:可能您希望在sum +下一个素数大于n之后立即停止而不添加另一个素数。

while True:
    if is_prime(i):
        if(sum + i > n)
            break;
        else:
            sum += i
            k += 1
    i += 1 
print(k)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM