简体   繁体   English

在python中打印一系列素数

[英]Print series of prime numbers in python

I was having issues in printing a series of prime numbers from one to hundred.我在打印从一到一百的一系列素数时遇到了问题。 I can't figure our what's wrong with my code.我无法弄清楚我的代码有什么问题。

Here's what I wrote;这是我写的; it prints all the odd numbers instead of primes:它打印所有奇数而不是素数:

for num in range(1, 101):
    for i in range(2, num):
        if num % i == 0:
            break
        else:
            print(num)
            break

You need to check all numbers from 2 to n-1 (to sqrt(n) actually, but ok, let it be n).您需要检查从 2 到 n-1 的所有数字(实际上到 sqrt(n),但是可以,让它成为 n)。 If n is divisible by any of the numbers, it is not prime.如果n能被任何数整除,则它不是素数。 If a number is prime, print it.如果一个数字是素数,打印它。

for num in range(2,101):
    prime = True
    for i in range(2,num):
        if (num%i==0):
            prime = False
    if prime:
       print (num)

You can write the same much shorter and more pythonic:你可以写同样的更短和更 Pythonic:

for num in range(2,101):
    if all(num%i!=0 for i in range(2,num)):
       print (num)

As I've said already, it would be better to check divisors not from 2 to n-1, but from 2 to sqrt(n):正如我已经说过的,最好检查除数不是从 2 到 n-1,而是从 2 到 sqrt(n):

import math
for num in range(2,101):
    if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):
       print (num)

For small numbers like 101 it doesn't matter, but for 10**8 the difference will be really big.对于像 101 这样的小数字,这无关紧要,但对于 10**8,差异会非常大。

You can improve it a little more by incrementing the range you check by 2, and thereby only checking odd numbers.您可以通过将您检查的范围增加 2 来进一步改进它,从而只检查奇数。 Like so:像这样:

import math
print 2
for num in range(3,101,2):
    if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):
       print (num)

Edited:编辑:

As in the first loop odd numbers are selected, in the second loop no need to check with even numbers, so 'i' value can be start with 3 and skipped by 2.由于在第一个循环中选择了奇数,在第二个循环中无需检查偶数,因此“i”值可以以 3 开头并跳过 2。

import math
print 2
for num in range(3,101,2):
    if all(num%i!=0 for i in range(3,int(math.sqrt(num))+1, 2)):
        print (num)

I'm a proponent of not assuming the best solution and testing it.我支持不假设最佳解决方案并对其进行测试。 Below are some modifications I did to create simple classes of examples by both @igor-chubin and @user448810.下面是我所做的一些修改,以通过@igor-chubin 和@user448810 创建简单的示例类。 First off let me say it's all great information, thank you guys.首先让我说这都是很好的信息,谢谢大家。 But I have to acknowledge @user448810 for his clever solution, which turns out to be the fastest by far (of those I tested).但我必须感谢@user448810 的聪明解决方案,结果证明这是迄今为止最快的(在我测试过的解决方案中)。 So kudos to you, sir!所以向你致敬,先生! In all examples I use a values of 1 million (1,000,000) as n.在所有示例中,我使用 100 万 (1,000,000) 作为 n。

Please feel free to try the code out.请随意尝试代码。

Good luck!祝你好运!

Method 1 as described by Igor Chubin: Igor Chubin 描述的方法 1

def primes_method1(n):
    out = list()
    for num in range(1, n+1):
        prime = True
        for i in range(2, num):
            if (num % i == 0):
                prime = False
        if prime:
            out.append(num)
    return out

Benchmark: Over 272+ seconds基准测试:超过 272 秒以上

Method 2 as described by Igor Chubin: Igor Chubin 描述的方法 2

def primes_method2(n):
    out = list()
    for num in range(1, n+1):
        if all(num % i != 0 for i in range(2, num)):
            out.append(num)
    return out

Benchmark: 73.3420000076 seconds基准: 73.3420000076 秒

Method 3 as described by Igor Chubin: Igor Chubin 描述的方法 3

def primes_method3(n):
    out = list()
    for num in range(1, n+1):
        if all(num % i != 0 for i in range(2, int(num**.5 ) + 1)):
            out.append(num)
    return out

Benchmark: 11.3580000401 seconds基准: 11.3580000401 秒

Method 4 as described by Igor Chubin: Igor Chubin 描述的方法 4

def primes_method4(n):
    out = list()
    out.append(2)
    for num in range(3, n+1, 2):
        if all(num % i != 0 for i in range(2, int(num**.5 ) + 1)):
            out.append(num)
    return out

Benchmark: 8.7009999752 seconds基准: 8.7009999752 秒

Method 5 as described by user448810 (which I thought was quite clever): user448810 描述的方法5 (我认为这很聪明):

def primes_method5(n):
    out = list()
    sieve = [True] * (n+1)
    for p in range(2, n+1):
        if (sieve[p]):
            out.append(p)
            for i in range(p, n+1, p):
                sieve[i] = False
    return out

Benchmark: 1.12000012398 seconds基准: 1.12000012398 秒

Notes: Solution 5 listed above (as proposed by user448810) turned out to be the fastest and honestly quiet creative and clever.注意:上面列出的解决方案 5(由 user448810 提出)结果证明是最快、最安静的创意和聪明的。 I love it.我喜欢它。 Thanks guys!!多谢你们!!

EDIT: Oh, and by the way, I didn't feel there was any need to import the math library for the square root of a value as the equivalent is just (n**.5).编辑:哦,顺便说一句,我觉得没有必要为值的平方根导入数学库,因为等价的只是 (n**.5)。 Otherwise I didn't edit much other then make the values get stored in and output array to be returned by the class.否则,我没有进行太多编辑,然后将值存储在输出数组中以由类返回。 Also, it would probably be a bit more efficient to store the results to a file than verbose and could save a lot on memory if it was just one at a time but would cost a little bit more time due to disk writes.此外,将结果存储到文件中可能比详细存储更有效,并且如果一次只存储一个文件可以节省大量内存,但由于磁盘写入会花费更多时间。 I think there is always room for improvement though.我认为总有改进的余地。 So hopefully the code makes sense guys.所以希望代码有意义。


2021 EDIT: I know it's been a really long time but I was going back through my Stackoverflow after linking it to my Codewars account and saw my recently accumulated points, which which was linked to this post. 2021 年编辑:我知道这已经很长时间了,但我在将 Stackoverflow 链接到我的 Codewars 帐户后返回并查看了我最近累积的积分,这些积分与这篇文章相关联。 Something I read in the original poster caught my eye for @user448810, so I decided to do a slight modification mentioned in the original post by filtering out odd values before appending the output array.我在原始海报中读到的东西引起了我对@user448810 的注意,因此我决定通过在附加输出数组之前过滤掉奇数值来对原始帖子中提到的进行轻微修改。 The results was much better performance for both the optimization as well as latest version of Python 3.8 with a result of 0.723 seconds (prior code) vs 0.504 seconds using 1,000,000 for n.结果是优化和最新版本的 Python 3.8 的性能要好得多,结果为 0.723 秒(之前的代码),而使用 1,000,000 的 n 为 0.504 秒。

def primes_method5(n):
    out = list()
    sieve = [True] * (n+1)
    for p in range(2, n+1):
        if (sieve[p] and sieve[p]%2==1):
            out.append(p)
            for i in range(p, n+1, p):
                sieve[i] = False
    return out

Nearly five years later, I might know a bit more but I still just love Python, and it's kind of crazy to think it's been that long.将近五年后,我可能知道得更多,但我仍然喜欢 Python,想想它已经这么久了有点疯狂。 The post honestly feels like it was made a short time ago and at the time I had only been using python about a year I think.老实说,这篇文章感觉就像是不久前发表的,当时我想我只使用 python 大约一年。 And it still seems relevant.它似乎仍然相关。 Crazy.疯狂的。 Good times.美好时光。

Instead of trial division, a better approach, invented by the Greek mathematician Eratosthenes over two thousand years ago, is to sieve by repeatedly casting out multiples of primes.两千多年前希腊数学家埃拉托色尼发明的一种更好的方法不是试除法,而是通过反复抛出多个素数来进行筛选。

Begin by making a list of all numbers from 2 to the maximum desired prime n.首先列出从 2 到最大所需素数 n 的所有数字。 Then repeatedly take the smallest uncrossed number and cross out all of its multiples;然后反复取最小的未交叉数并将其所有倍数划掉; the numbers that remain uncrossed are prime.未交叉的数字是素数。

For example, consider the numbers less than 30. Initially, 2 is identified as prime, then 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28 and 30 are crossed out.例如,考虑小于 30 的数字。最初,2 被识别为素数,然后将 4、6、8、10、12、14、16、18、20、22、24、26、28 和 30 划掉。 Next 3 is identified as prime, then 6, 9, 12, 15, 18, 21, 24, 27 and 30 are crossed out.接下来的 3 被确定为素数,然后 6、9、12、15、18、21、24、27 和 30 被划掉。 The next prime is 5, so 10, 15, 20, 25 and 30 are crossed out.下一个素数是 5,因此 10、15、20、25 和 30 被划掉。 And so on.等等。 The numbers that remain are prime: 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.剩下的数是素数:2、3、5、7、11、13、17、19、23 和 29。

def primes(n):
  sieve = [True] * (n+1)
  for p in range(2, n+1):
    if (sieve[p]):
      print p
      for i in range(p, n+1, p):
        sieve[i] = False

An optimized version of the sieve handles 2 separately and sieves only odd numbers.优化版的筛子分别处理 2 个并且只筛分奇数。 Also, since all composites less than the square of the current prime are crossed out by smaller primes, the inner loop can start at p^2 instead of p and the outer loop can stop at the square root of n.此外,由于所有小于当前素数平方的复合都被较小的素数划掉,所以内循环可以从 p^2 开始,而不是从 p 开始,外循环可以在 n 的平方根处停止。 I'll leave the optimized version for you to work on.我会把 优化的版本留给你处理。

break ends the loop that it is currently in. So, you are only ever checking if it divisible by 2, giving you all odd numbers. break结束它当前所在的循环。所以,你只检查它是否能被 2 整除,给你所有奇数。

for num in range(2,101):
    for i in range(2,num):
        if (num%i==0):
            break
    else:
        print(num)

that being said, there are much better ways to find primes in python than this.话虽如此,在 python 中找到素数还有比这更好的方法。

for num in range(2,101):
    if is_prime(num):
        print(num)

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

The best way to solve the above problem would be to use the "Miller Rabin Primality Test" algorithm.解决上述问题的最佳方法是使用“Miller Rabin Primality Test”算法。 It uses a probabilistic approach to find if a number is prime or not.它使用概率方法来确定一个数字是否为素数。 And it is by-far the most efficient algorithm I've come across for the same.它是迄今为止我遇到过的最有效的算法。

The python implementation of the same is demonstrated below:同样的python实现如下所示:

def miller_rabin(n, k):

    # Implementation uses the Miller-Rabin Primality Test
    # The optimal number of rounds for this test is 40
    # See http://stackoverflow.com/questions/6325576/how-many-iterations-of-rabin-miller-should-i-use-for-cryptographic-safe-primes
    # for justification

    # If number is even, it's a composite number

    if n == 2:
        return True

    if n % 2 == 0:
        return False

    r, s = 0, n - 1
    while s % 2 == 0:
        r += 1
        s //= 2
    for _ in xrange(k):
        a = random.randrange(2, n - 1)
        x = pow(a, s, n)
        if x == 1 or x == n - 1:
            continue
        for _ in xrange(r - 1):
            x = pow(x, 2, n)
            if x == n - 1:
                break
        else:
            return False
    return True

Igor Chubin 's answer can be improved. Igor Chubin的回答可以改进。 When testing if X is prime, the algorithm doesn't have to check every number up to the square root of X, it only has to check the prime numbers up to the sqrt(X).在测试 X 是否为素数时,算法不必检查直到 X 的平方根的每个数,它只需要检查直到 sqrt(X) 的素数。 Thus, it can be more efficient if it refers to the list of prime numbers as it is creating it.因此,如果它在创建质数列表时引用质数列表,则效率会更高。 The function below outputs a list of all primes under b, which is convenient as a list for several reasons (eg when you want to know the number of primes < b).下面的函数输出 b 下所有素数的列表,作为列表方便有几个原因(例如,当您想知道素数 < b 的数量时)。 By only checking the primes, it saves time at higher numbers (compare at around 10,000; the difference is stark).通过只检查素数,它可以在更高的数字上节省时间(与大约 10,000 相比;差异非常明显)。

from math import sqrt
def lp(b)
    primes = [2]
    for c in range(3,b):
        e = round(sqrt(c)) + 1
        for d in primes:
            if d <= e and c%d == 0:
                break
        else:
            primes.extend([c])
    return primes

My way of listing primes to an entry number without too much hassle is using the property that you can get any number that is not a prime with the summation of primes.我将素数列出到一个条目数字而没有太多麻烦的方法是使用这个属性,您可以通过素数的总和获得任何不是素数的数字。

Therefore, if you divide the entry number with all primes below it, and it is not evenly divisible by any of them, you know that you have a prime.因此,如果将条目数除以它下面的所有素数,并且它不能被它们中的任何一个整除,那么你就知道你有一个素数。

Of course there are still faster ways of getting the primes, but this one already performs quite well, especially because you are not dividing the entry number by any number, but quite only the primes all the way to that number.当然还有更快的获得素数的方法,但是这个已经表现得很好,特别是因为你没有将条目数除以任何数字,而只是将素数一直到那个数字。

With this code I managed on my computer to list all primes up to 100 000 in less than 4 seconds.使用此代码,我在计算机上设法在不到 4 秒的时间内列出了最多 100 000 个的所有素数。

import time as t

start = t.clock()

primes = [2,3,5,7]

for num in xrange(3,100000,2):
    if all(num%x != 0 for x in primes):
        primes.append(num)

print primes
print t.clock() - start
print sum(primes)

A Python Program function module that returns the 1'st N prime numbers:返回第一个 N 素数的 Python 程序函数模块:

def get_primes(count):
    """
        Return the 1st count prime integers.
    """
    result = []
    x=2
    while len(result) in range(count):
        i=2
        flag=0
        for i in range(2,x):
            if x%i == 0:
                flag+=1
                break
            i=i+1
        if flag == 0:
            result.append(x)
        x+=1
    pass
    return result

A simpler and more efficient way of solving this is storing all prime numbers found previously and checking if the next number is a multiple of any of the smaller primes.解决这个问题的一种更简单、更有效的方法是存储之前找到的所有素数,并检查下一个数字是否是任何较小素数的倍数。

n = 1000
primes = [2]

for i in range(3, n, 2):
    if not any(i % prime == 0 for prime in primes):
        primes.append(i)

print(primes)

Note that any is a short circuit function, in other words, it will break the loop as soon as a truthy value is found.请注意, any是一个短路函数,换句话说,一旦找到真值,它就会中断循环。

we can make a list of prime numbers using sympy library我们可以使用 sympy 库制作一个素数列表

import sympy
lower=int(input("lower value:"))          #let it be 30
upper=int(input("upper value:"))          #let it be 60
l=list(sympy.primerange(lower,upper+1))   #[31,37,41,43,47,53,59]
print(l)

Here's a simple and intuitive version of checking whether it's a prime in a RECURSIVE function!这是一个简单直观的检查它是否是 RECURSIVE 函数中的素数的版本! :) (I did it as a homework assignment for an MIT class) In python it runs very fast until 1900. IF you try more than 1900, you'll get an interesting error :) (Would u like to check how many numbers your computer can manage?) :) (我是作为 MIT 课程的家庭作业完成的)在 python 中它运行得非常快,直到 1900 年。如果你尝试超过 1900 年,你会得到一个有趣的错误 :) (你想检查你的数字有多少?电脑能管理吗?)

def is_prime(n, div=2):

    if div> n/2.0: return True

    if n% div == 0:
        return False
    else:
        div+=1
        return is_prime(n,div)

#The program:
until = 1000
for i in range(until):
    if is_prime(i):
        print i

Of course... if you like recursive functions, this small code can be upgraded with a dictionary to seriously increase its performance, and avoid that funny error.当然……如果你喜欢递归函数,这个小代码可以用字典来升级,以大大提高它的性能,并避免那个有趣的错误。 Here's a simple Level 1 upgrade with a MEMORY integration:这是带有 MEMORY 集成的简单 1 级升级:

import datetime
def is_prime(n, div=2):
    global primelist
    if div> n/2.0: return True
    if div < primelist[0]:
        div = primelist[0]
        for x in primelist:
            if x ==0 or x==1: continue
            if n % x == 0:
                return False
    if n% div == 0:
        return False
    else:
        div+=1
        return is_prime(n,div)


now = datetime.datetime.now()
print 'time and date:',now
until = 100000
primelist=[]
for i in range(until):
    if is_prime(i):
        primelist.insert(0,i)
print "There are", len(primelist),"prime numbers, until", until
print primelist[0:100], "..."

finish = datetime.datetime.now()
print "It took your computer", finish - now , " to calculate it"

Here are the resuls, where I printed the last 100 prime numbers found.这是结果,我打印了找到的最后 100 个素数。

time and date: 2013-10-15 13:32:11.674448时间和日期:2013-10-15 13:32:11.674448

There are 9594 prime numbers, until 100000有 9594 个素数,直到 100000

[99991, 99989, 99971, 99961, 99929, 99923, 99907, 99901, 99881, 99877, 99871, 99859, 99839, 99833, 99829, 99823, 99817, 99809, 99793, 99787, 99767, 99761, 99733, 99721, 99719, 99713, 99709, 99707, 99689, 99679, 99667, 99661, 99643, 99623, 99611, 99607, 99581, 99577, 99571, 99563, 99559, 99551, 99529, 99527, 99523, 99497, 99487, 99469, 99439, 99431, 99409, 99401, 99397, 99391, 99377, 99371, 99367, 99349, 99347, 99317, 99289, 99277, 99259, 99257, 99251, 99241, 99233, 99223, 99191, 99181, 99173, 99149, 99139, 99137, 99133, 99131, 99119, 99109, 99103, 99089, 99083, 99079, 99053, 99041, 99023, 99017, 99013, 98999, 98993, 98981, 98963, 98953, 98947, 98939, 98929, 98927, 98911, 98909, 98899, 98897] ... [99991, 99989, 99971, 99961, 99929, 99923, 99907, 99901, 99881, 99877, 99871, 99859, 99839, 99833, 99829, 99823, 99817, 99809, 99793, 99787, 99767, 99761, 99733, 99721, 99719 , 99713, 99709, 99707, 99689, 99679, 99667, 99661, 99643, 99623, 99611, 99607, 99581, 99577, 99571, 99563, 99559, 99551, 99529, 99527, 99523, 99497, 99487, 99469, 99439, 99431 , 99409, 99401, 99397, 99391, 99377, 99371, 99367, 99349, 99347, 99317, 99289, 99277, 99259, 99257, 99251, 99241, 99233, 99223, 99191, 99181, 99173, 99149, 99139, 99137, 99133 , 99131, 99119, 99109, 99103, 99089, 99083, 99079, 99053, 99041, 99023, 99017, 99013, 98999, 98993, 98981, 98963, 98953, 98947, 98939, 98929, 98927, 98911, 98909, 98899, 98897 ] ...

It took your computer 0:00:40.871083 to calculate it你的电脑用了 0:00:40.871083 来计算它

So It took 40 seconds for my i7 laptop to calculate it.所以我的 i7 笔记本电脑花了 40 秒来计算它。 :) :)

# computes first n prime numbers
def primes(n=1):
    from math import sqrt
    count = 1
    plist = [2]
    c = 3
    if n <= 0 :
        return "Error : integer n not >= 0"
    while (count <= n - 1):    # n - 1 since 2 is already in plist
        pivot = int(sqrt(c))
        for i in plist:
            if i > pivot :    # check for primae factors 'till sqrt c
                count+= 1
                plist.append(c)
                break
            elif c % i == 0 :
                break    # not prime, no need to iterate anymore
            else :
                continue 
        c += 2    # skipping even numbers              
    return plist

You are terminating the loop too early.您过早地终止循环。 After you have tested all possibilities in the body of the for loop, and not breaking, then the number is prime.在你测试了 for 循环体中的所有可能性并且没有中断之后,这个数字就是素数。 As one is not prime you have to start at 2:由于一个不是素数,你必须从 2 开始:

for num in xrange(2, 101):
    for i in range(2,num):
        if not num % i:
            break
    else:
        print num

In a faster solution you only try to divide by primes that are smaller or equal to the root of the number you are testing.在更快的解决方案中,您只尝试除以小于或等于您正在测试的数字的根的素数。 This can be achieved by remembering all primes you have already found.这可以通过记住你已经找到的所有素数来实现。 Additionally, you only have to test odd numbers (except 2).此外,您只需测试奇数(2 除外)。 You can put the resulting algorithm into a generator so you can use it for storing primes in a container or simply printing them out:您可以将生成的算法放入生成器中,以便将其用于将素数存储在容器中或简单地将它们打印出来:

def primes(limit):
    if limit > 1:
        primes_found = [(2, 4)]
        yield 2
        for n in xrange(3, limit + 1, 2):
            for p, ps in primes_found:
                if ps > n:
                    primes_found.append((n, n * n))
                    yield n
                    break
                else:
                    if not n % p:
                        break

for i in primes(101):
    print i

As you can see there is no need to calculate the square root, it is faster to store the square for each prime number and compare each divisor with this number.如您所见,无需计算平方根,存储每个素数的平方并将每个除数与该数字进行比较会更快。

How about this?这个怎么样? Reading all the suggestions I used this:阅读我使用的所有建议:

prime=[2]+[num for num in xrange(3,m+1,2) if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1))]

Prime numbers up to 1000000质数高达 1000000

root@nfs:/pywork# time python prime.py

78498 78498

real 0m6.600s真正的 0m6.600s

user 0m6.532s用户 0m6.532s

sys 0m0.036s系统 0m0.036s

Adding to the accepted answer, further optimization can be achieved by using a list to store primes and printing them after generation.除了已接受的答案之外,还可以通过使用列表来存储素数并在生成后打印它们来实现进一步的优化。

import math
Primes_Upto = 101
Primes = [2]
for num in range(3,Primes_Upto,2):
    if all(num%i!=0 for i in Primes):
       Primes.append(num)
for i in Primes:
    print i

Here is the simplest logic for beginners to get prime numbers:这是初学者获取素数的最简单逻辑:

p=[]
for n in range(2,50):
    for k in range(2,50):
        if n%k ==0 and n !=k:
            break
        else:
            for t in p:
                if  n%t ==0:
                    break
            else:
                p.append(n)

print p
n = int(input())
is_prime = lambda n: all( n%i != 0 for i in range(2, int(n**.5)+1) )
def Prime_series(n):
    for i in range(2,n):
        if is_prime(i) == True:
            print(i,end = " ")
        else:
            pass
Prime_series(n)

Here is a simplified answer using lambda function.这是使用 lambda 函数的简化答案。

def function(number):
    for j in range(2, number+1):
        if all(j % i != 0 for i in range(2, j)):
            print(j)


function(13)

Print n prime numbers using python:使用 python 打印 n 个素数:

num = input('get the value:')
for i in range(2,num+1):
    count = 0
    for j in range(2,i):
        if i%j != 0:
            count += 1
    if count == i-2:
        print i,
def prime_number(a):
    yes=[]
    for i in range (2,100):
        if (i==2 or i==3 or i==5 or i==7) or (i%2!=0 and i%3!=0 and i%5!=0 and i%7!=0 and i%(i**(float(0.5)))!=0):
            yes=yes+[i]
    print (yes)
min=int(input("min:"))
max=int(input("max:"))
for num in range(min,max):
    for x in range(2,num):
        if(num%x==0 and num!=1):
            break
        else:
            print(num,"is prime")
            break

This is a sample program I wrote to check if a number is prime or not.这是我编写的一个示例程序,用于检查一个数字是否为素数。

def is_prime(x):
    y=0
    if x<=1:
        return False
    elif x == 2:
        return True
    elif x%2==0:
        return False
    else:
        root = int(x**.5)+2
        for i in xrange (2,root):
            if x%i==0:
                return False
                y=1
        if y==0:
            return True
n = int(raw_input('Enter the integer range to find prime no :'))
p = 2
while p<n:
  i = p
  cnt = 0
  while i>1:
    if p%i == 0:
        cnt+=1
    i-=1
  if cnt == 1:
     print "%s is Prime Number"%p
  else:
     print "%s is Not Prime Number"%p
  p+=1

Using filter function.使用过滤功能。

l=range(1,101)
for i in range(2,10): # for i in range(x,y), here y should be around or <= sqrt(101)
    l = filter(lambda x: x==i or x%i, l)

print l
for num in range(1,101):
    prime = True
    for i in range(2,num/2):
        if (num%i==0):
            prime = False
    if prime:
       print num

Adding my own version, just to show some itertools tricks v2.7:添加我自己的版本,只是为了展示一些 itertools 技巧 v2.7:

import itertools

def Primes():
    primes = []
    a = 2
    while True:
        if all(itertools.imap(lambda p : a % p, primes)):
            yield a
            primes.append(a)
        a += 1

# Print the first 100 primes
for _, p in itertools.izip(xrange(100), Primes()):
    print p
f=0
sum=0
for i in range(1,101):
    for j in range(1,i+1):
        if(i%j==0):
            f=f+1
    if(f==2):
        sum=sum+i
        print i        
    f=0
print sum

The fastest & best implementation of omitting primes:省略素数的最快和最佳实现:

def PrimeRanges2(a, b):
    arr = range(a, b+1)
    up = int(math.sqrt(b)) + 1
    for d in range(2, up):
        arr = omit_multi(arr, d)

Here is a different approach that trades space for faster search time.这是一种不同的方法,可以用空间换取更快的搜索时间。 This may be fastest so.这可能是最快的。

import math

def primes(n):
    if n < 2:
        return []
    numbers = [0]*(n+1)
    primes = [2]
    # Mark all odd numbers as maybe prime, leave evens marked composite.
    for i in xrange(3, n+1, 2):
        numbers[i] = 1

    sqn = int(math.sqrt(n))
    # Starting with 3, look at each odd number.
    for i in xrange(3, len(numbers), 2):
        # Skip if composite.
        if numbers[i] == 0:
            continue
        # Number is prime.  Would have been marked as composite if there were
        # any smaller prime factors already examined.
        primes.append(i)
        if i > sqn:
            # All remaining odd numbers not marked composite must be prime.
            primes.extend([i for i in xrange(i+2, len(numbers), 2)
                           if numbers[i]])
            break
        # Mark all multiples of the prime as composite.  Check odd multiples.
        for r in xrange(i*i, len(numbers), i*2):
            numbers[r] = 0

    return primes

n = 1000000
p = primes(n)
print "Found", len(p), "primes <=", n

I was inspired by Igor and made a code block that creates a list:我受到 Igor 的启发,制作了一个创建列表的代码块:

def prime_number():

for num in range(2, 101):
    prime = True
    for i in range(2, num):
        if (num % i == 0):
            prime = False
    if prime and num not in num_list:
        num_list.append(num)
    else:
        pass
return num_list


num_list = []
prime_number()
print(num_list)
a=int(input('enter the lower no.'))
b=int(input('enter the higher no.'))
print("Prime numbers between",a,"and",b,"are:")
for num in range(a,b):

    if num>1:
        for i in range(2,num):
            if (num%i)==0:
                break
        else:
            print(num)

First we find factor of that number首先我们找到那个数字的因数

def fac(n):
  res = []
  for i in range(1,n+1):
    if n%i == 0:
res.append(i)

Script to check prime or not检查素数的脚本

def prime(n):
return(fac(n) == [1,n])

Script to print all prime number upto n打印所有素数到 n 的脚本

def prime_list(n):
  pri_list = []
  for i in range(1,n+1):
    if prime(i)
      pri_list.append(i)
return(pri_list)
for i in range(1, 100):
  for j in range(2, i):
    if i % j == 0:
      break 
  else:
    print(i)

as one line comprehension list solution:作为一个行理解列表解决方案:

>>> [num for num in range(2, 101) if all(num % i != 0 for i in range(2, num))]
[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]

which 101 represent the maximum range value between 2 and 100 of list其中101表示列表的2100之间的最大范围值

1 can only be divided by one number, 1 itself, so with this definition 1 is not a prime number. 1只能除以一个数,即1本身,因此根据这个定义, 1不是素数。 The primary number must have only two positive factors 1 and itself .主数必须只有两个正因数1itself

num= int(input("Enter the numbner"))
isDivisible= False
int=2

while i<num:
    if num%i==0
    isDivisible True
    print("The number {} is divisible by {}.".format(num,i))
    i +=1

if isDivisible:
    print("The number {} is not prime.".format(num))
else:
    print("The number {} is a prime number.".format(num))

min = int(input("Enter lower range: ")) max = int(input("Enter upper range: ")) min = int(input("输入下限:")) max = int(input("输入上限:"))

print("The Prime numbes between",min,"and",max,"are:" print("",min,"和",max,"之间的素数是:"

for num in range(min,max + 1): if num > 1: for i in range(2,num): if (num % i) == 0: break else: print(num) for num in range(min,max + 1): if num > 1: for i in range(2,num): if (num % i) == 0: break else: print(num)

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

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