简体   繁体   English

质数小于或等于n

[英]Prime numbers less than or equal to n

The statement says: 声明说:

Write a function era1() that ask the user for a number n and then use this algorithm to print all prime numbers less than or equal to n . 编写一个函数era1() ,要求用户输入数字n ,然后使用此算法打印所有小于或等于n素数。

Algorithm: 算法:

  • Write a list with numbers from 2 to largest integer N you want to calculate. 编写一个包含从2到要计算的最大整数N的数字的列表。
  • The first number in the list is a prime number. 列表中的第一个数字是质数。 Write this number a list of primes, B . 将这个数字写成素数列表B
  • Remove from the list A , the first element and its multiples. 从列表A删除第一个元素及其倍数。
  • If the first number in the list A is less than the square root N, come back to second point. 如果列表A的第一个数字小于平方根N,请返回第二点。
  • The numbers in the B list and those left in List A are all primes searched. B列表中的数字和列表A中剩余的数字都是素数。

Now, I put this code: 现在,我输入以下代码:

import math

def primo(num):
    if num < 2:
        return False

    i = 2
    for i in range(2, int(math.sqrt(num) + 1)):
        if (num % i == 0):
            return False

    return True

def main():
    n = input("Introdueix un nombre: ")
    B = range(2, n)
    for i in B:
        if primo(i):
            print i        

main()

def era1():
    n = input("Introdueix un nombre: ")
    A = range(2, n + 1)
    B = [A[0]]

    for i in A:
        if i % 2 == 0:
            A.remove(i)

    if A[0] < math.sqrt(n):
        print B + A

era1()

The result is incorrect because if I remove one of the input, appears error and I have to put only one time the input. 结果不正确,因为如果删除输入之一,则会出现错误,并且只需要输入一次即可。 Also the result is incorrect because the A + B , the list B is not the list of function main and the final result is only the numbers not multiples of 2 and 2. How can I put only one input and then the final result will be correct? 而且结果是不正确的,因为A + B ,列表B不是函数main的列表,最终结果只是数字而不是2和2的倍数。正确?

This Algorithm is called Sieve of Eratosthenes . 该算法称为Eratosthenes筛网

It is a simple algorithm for finding all prime numbers up to a specified integer. 这是一种简单的算法,可查找所有最大为指定整数的质数。 It was created in the 3rd century BC by Eratosthenes, an ancient Greek mathematician. 它是由古希腊数学家Eratosthenes在公元前3世纪创建的。

In order to develop this algorithm, we'll go through the different above-mentioned steps. 为了开发此算法,我们将经历上述不同的步骤。

  • First, we generate a list with numbers from 2 to largest integer N you want to calculate. 首先,我们生成一个列表,其中包含从2到要计算的最大整数N的数字。
 A = range(2, n + 1) 
  • We use another list C, as we might use A later to print the initial list. 我们使用另一个列表C,因为稍后可能会使用A打印初始列表。

  • We go through C, processing all the numbers less than the square root N. 我们经过C,处理所有小于平方根N的数字。

  • We initialize an empty list B, and add every time a prime number (Which is first element of the list). 我们初始化一个空列表B,然后每次添加一个质数(哪个是列表的第一个元素)。
  • We use list comprehension to filter multiplies using : (x%firstElement!=0) . 我们使用列表(x%firstElement!=0)来过滤使用(x%firstElement!=0)乘法。

C= [x for x in C if x%firstElement!=0] C = [如果x%firstElement!= 0,则表示C中x的x]

  • B is the union of the rest of numbers (prime numbers that are larger than the square root N), and the prime numbers that we detected already. B是其余数字(大于平方根N的素数)和我们已经检测到的素数的并集。

Your code should look like: 您的代码应如下所示:

def era1():
    n = input("Introduce a nombre: ")
    #n=120 #To test the
    A = range(2, n + 1) 
    B, C= [],A
    while C[0]< math.sqrt(n): #Condition
        firstElement= C[0]
        B+= [firstElement] #The first number in the list is a prime number. Write this number a list of primes, B.
        C= [x for x in C if x%firstElement!=0] #We use comprehension List to filter multiplies using
    return B+C #The numbers in the B list and those left in List A are all primes searched.

print era1()

Output in case of n=120: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113] n = 120时的输出:[2、3、5、7、11、13、17、19、23、29、31、37、41、43、47、53、59、61、67、71、73 ,79、83、89、97、101、103、107、109、113]

在此处输入图片说明

This picture visualize the Algorithm, Source of picture . 该图片可视化算法, 图片来源

Removing items from a list while iterating over it will have unexpected results, it interferes with the indexing. 在列表上进行迭代时从列表中删除项目将产生意外结果,这会干扰索引编制。

a = [1,2,3,4,5,6,7,8,9]
for thing in a:
    a.remove(thing)


>>> a
[2, 4, 6, 8]
>>> 

You will need to figure out another way to accomplish that - perhaps create a new list with the items you want to keep. 您将需要找出另一种方法来完成此操作-也许用您想要保留的项目创建一个新列表。

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

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