Python中的简单Prime生成器

时间:2009-02-19 21:22:25

标签: python primes

有人可以告诉我这段代码我做错了什么吗?无论如何,它只是打印'计数'。我只想要一个非常简单的素数发生器(没什么特别的)。

import math

def main():
    count = 3
    one = 1
    while one == 1:
        for x in range(2, int(math.sqrt(count) + 1)):
            if count % x == 0: 
                continue
            if count % x != 0:
                print count

        count += 1

26 个答案:

答案 0 :(得分:142)

存在一些问题:

  • 为什么在不除以x时打印出计数?它并不意味着它是素数,它只意味着这个特定的x不会分裂它
  • continue转到下一个循环迭代 - 但你真的想用break
  • 来阻止它

这是你的代码,有一些修复,它只打印出素数:

import math

def main():
    count = 3
    
    while True:
        isprime = True
        
        for x in range(2, int(math.sqrt(count) + 1)):
            if count % x == 0: 
                isprime = False
                break
        
        if isprime:
            print count
        
        count += 1

对于更高效的素数生成,请参阅其他人建议的Erastothenes筛选。这是一个很好的,优化的实现,有很多注释:

# Sieve of Eratosthenes
# Code by David Eppstein, UC Irvine, 28 Feb 2002
# http://code.activestate.com/recipes/117119/

def gen_primes():
    """ Generate an infinite sequence of prime numbers.
    """
    # Maps composites to primes witnessing their compositeness.
    # This is memory efficient, as the sieve is not "run forward"
    # indefinitely, but only as long as required by the current
    # number being tested.
    #
    D = {}
    
    # The running integer that's checked for primeness
    q = 2
    
    while True:
        if q not in D:
            # q is a new prime.
            # Yield it and mark its first multiple that isn't
            # already marked in previous iterations
            # 
            yield q
            D[q * q] = [q]
        else:
            # q is composite. D[q] is the list of primes that
            # divide it. Since we've reached q, we no longer
            # need it in the map, but we'll mark the next 
            # multiples of its witnesses to prepare for larger
            # numbers
            # 
            for p in D[q]:
                D.setdefault(p + q, []).append(p)
            del D[q]
        
        q += 1

请注意,它会返回一个生成器。

答案 1 :(得分:12)

def is_prime(num):
    """Returns True if the number is prime
    else False."""
    if num == 0 or num == 1:
        return False
    for x in range(2, num):
        if num % x == 0:
            return False
    else:
        return True

>> filter(is_prime, range(1, 20))
  [2, 3, 5, 7, 11, 13, 17, 19]

我们将在列表中获得最多20个素数。 我本来可以用过Eratosthenes的Sieve,但是你说 你想要一些非常简单的东西;)

答案 2 :(得分:7)

重要的是:

import re


def isprime(n):
    return re.compile(r'^1?$|^(11+)\1+$').match('1' * n) is None

print [x for x in range(100) if isprime(x)]

###########Output#############
[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]

答案 3 :(得分:7)

print [x for x in range(2,100) if not [t for t in range(2,x) if not x%t]]

答案 4 :(得分:4)

def primes(n): # simple Sieve of Eratosthenes 
   odds = range(3, n+1, 2)
   sieve = set(sum([range(q*q, n+1, q+q) for q in odds],[]))
   return [2] + [p for p in odds if p not in sieve]

>>> primes(50)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

测试一个数字是否为素数:

>>> 541 in primes(541)
True
>>> 543 in primes(543)
False

答案 5 :(得分:3)

这是一个简单(Python 2.6.2)解决方案......它符合OP的原始请求(现在已有六个月的历史);并且应该是任何“编程101”课程中完全可以接受的解决方案......因此这篇文章。

import math

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

print 2
for n in range(3, 50):
    if isPrime(n):
        print n

这种简单的“蛮力”方法对于现代PC上的数字大约为16,000的数字来说“足够快”(在我的2GHz机箱上大约需要8秒)。

显然,这可以通过不重新计算每个偶数的素数,或每个数字的3,5,7等的每个倍数来更有效地完成...参见Sieve of Eratosthenes(见如果你感到特别勇敢和/或疯狂,那么eliben在上面的实现,甚至是Sieve of Atkin

警告:我是一个蟒蛇诺布。请不要把我说的任何东西当作福音。

答案 6 :(得分:2)

python 3(生成素数)

import math

i = 2
while True:
    for x in range(2, int(math.sqrt(i) + 1)):
        if i%x==0:
            break
    else:
        print(i)
    i += 1

答案 7 :(得分:2)

我认为最好采用功能性方法,

所以我首先创建一个函数,以确定数字是否为素数,然后根据需要在循环或其他位置使用它。

def isprime(n):
      for x in range(2,n):
        if n%x == 0:
            return False
    return True

然后运行一个简单的列表推导或生成器表达式来获取素数列表

[x for x in range(1,100) if isprime(x)]

答案 8 :(得分:1)

这似乎是作业,所以我会给出一个提示而不是详细解释。如果我认为错了,请纠正我。

当你看到一个均衡的除数时,你做得很好。

但是,只要你看到一个号码并不分成它,你就会打印'计数'。例如,2不能均匀地划分为9.但这并不能使9成为素数。您可能希望继续前进,直到您确定范围匹配中的号码。

(正如其他人所回答的那样,Sieve是一种更有效的方式......只是试图帮助你理解为什么这个特定代码没有做你想做的事情)

答案 9 :(得分:1)

仅研究了该主题,在线程中查找示例并尝试制作我的版本:

from collections import defaultdict
# from pprint import pprint

import re


def gen_primes(limit=None):
    """Sieve of Eratosthenes"""
    not_prime = defaultdict(list)
    num = 2
    while limit is None or num <= limit:
        if num in not_prime:
            for prime in not_prime[num]:
                not_prime[prime + num].append(prime)
            del not_prime[num]
        else:  # Prime number
            yield num
            not_prime[num * num] = [num]
        # It's amazing to debug it this way:
        # pprint([num, dict(not_prime)], width=1)
        # input()
        num += 1


def is_prime(num):
    """Check if number is prime based on Sieve of Eratosthenes"""
    return num > 1 and list(gen_primes(limit=num)).pop() == num


def oneliner_is_prime(num):
    """Simple check if number is prime"""
    return num > 1 and not any([num % x == 0 for x in range(2, num)])


def regex_is_prime(num):
    return re.compile(r'^1?$|^(11+)\1+$').match('1' * num) is None


def simple_is_prime(num):
    """Simple check if number is prime
    More efficient than oneliner_is_prime as it breaks the loop
    """
    for x in range(2, num):
        if num % x == 0:
            return False
    return num > 1


def simple_gen_primes(limit=None):
    """Prime number generator based on simple gen"""
    num = 2
    while limit is None or num <= limit:
        if simple_is_prime(num):
            yield num
        num += 1


if __name__ == "__main__":
    less1000primes = list(gen_primes(limit=1000))
    assert less1000primes == list(simple_gen_primes(limit=1000))
    for num in range(1000):
        assert (
            (num in less1000primes)
            == is_prime(num)
            == oneliner_is_prime(num)
            == regex_is_prime(num)
            == simple_is_prime(num)
        )
    print("Primes less than 1000:")
    print(less1000primes)

    from timeit import timeit

    print("\nTimeit:")
    print(
        "gen_primes:",
        timeit(
            "list(gen_primes(limit=1000))",
            setup="from __main__ import gen_primes",
            number=1000,
        ),
    )
    print(
        "simple_gen_primes:",
        timeit(
            "list(simple_gen_primes(limit=1000))",
            setup="from __main__ import simple_gen_primes",
            number=1000,
        ),
    )
    print(
        "is_prime:",
        timeit(
            "[is_prime(num) for num in range(2, 1000)]",
            setup="from __main__ import is_prime",
            number=100,
        ),
    )
    print(
        "oneliner_is_prime:",
        timeit(
            "[oneliner_is_prime(num) for num in range(2, 1000)]",
            setup="from __main__ import oneliner_is_prime",
            number=100,
        ),
    )
    print(
        "regex_is_prime:",
        timeit(
            "[regex_is_prime(num) for num in range(2, 1000)]",
            setup="from __main__ import regex_is_prime",
            number=100,
        ),
    )
    print(
        "simple_is_prime:",
        timeit(
            "[simple_is_prime(num) for num in range(2, 1000)]",
            setup="from __main__ import simple_is_prime",
            number=100,
        ),
    )

运行此代码的结果显示出有趣的结果:

$ python prime_time.py
Primes less than 1000:
[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, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]

Timeit:
gen_primes: 0.6738066330144648
simple_gen_primes: 4.738092333020177
is_prime: 31.83770858097705
oneliner_is_prime: 3.3708438930043485
regex_is_prime: 8.692703998007346
simple_is_prime: 0.4686249239894096

因此,我可以看到我们对不同的问题有正确的答案;对于质数生成器gen_primes来说似乎是正确的答案;但是对于素数检查,simple_is_prime函数更合适。

这可行,但是我总是乐于尝试更好的方法来实现is_prime功能。

答案 10 :(得分:1)

这是我的实现。我确定有一种更有效的方法,但似乎可以工作。基本标志使用。

def genPrime():
    num = 1
    prime = False
    while True:
        # Loop through all numbers up to num
        for i in range(2, num+1):
            # Check if num has remainder after the modulo of any previous numbers
            if num % i == 0:
                prime = False
                # Num is only prime if no remainder and i is num
                if i == num:
                    prime = True
                break

        if prime:
            yield num
            num += 1
        else:
            num += 1

prime = genPrime()
for _ in range(100):
    print(next(prime))

答案 11 :(得分:1)

另一个简单的例子,只考虑奇数的简单优化。一切都是用懒惰的流(python生成器)完成的。

用法:primes = list(create_prime_iterator(1,30))

import math
import itertools

def create_prime_iterator(rfrom, rto):
    """Create iterator of prime numbers in range [rfrom, rto]"""
    prefix = [2] if rfrom < 3 and rto > 1 else [] # include 2 if it is in range separately as it is a "weird" case of even prime
    odd_rfrom = 3 if rfrom < 3 else make_odd(rfrom) # make rfrom an odd number so that  we can skip all even nubers when searching for primes, also skip 1 as a non prime odd number.
    odd_numbers = (num for num in xrange(odd_rfrom, rto + 1, 2))
    prime_generator = (num for num in odd_numbers if not has_odd_divisor(num))
    return itertools.chain(prefix, prime_generator)

def has_odd_divisor(num):
    """Test whether number is evenly divisable by odd divisor."""
    maxDivisor = int(math.sqrt(num))
    for divisor in xrange(3, maxDivisor + 1, 2):
        if num % divisor == 0:
            return True
    return False

def make_odd(number):
    """Make number odd by adding one to it if it was even, otherwise return it unchanged"""
    return number | 1

答案 12 :(得分:1)

如果你想直接计算素数,那该怎么做:

def oprime(n):
counter = 0
b = 1
if n == 1:
    print 2
while counter < n-1:
    b = b + 2
    for a in range(2,b):
        if b % a == 0:
            break
    else:
        counter = counter + 1
        if counter == n-1:
            print b

答案 13 :(得分:0)

与user107745类似,但使用'all'而不是double negation(稍微更具可读性,但我认为性能相同):

import math
[x for x in xrange(2,10000) if all(x%t for t in xrange(2,int(math.sqrt(x))+1))]

基本上它在(2,100)的范围内迭代x并且只选择那些对于范围内的所有t没有mod == 0的那些(2,x)

另一种方式可能只是填充素数:

primes = set()
def isPrime(x):
  if x in primes:
    return x
  for i in primes:
    if not x % i:
      return None
  else:
    primes.add(x)
    return x

filter(isPrime, range(2,10000))

答案 14 :(得分:0)

def genPrimes():
    primes = []   # primes generated so far
    last = 1      # last number tried
    while True:
        last += 1
        for p in primes:
            if last % p == 0:
                break
        else:
            primes.append(last)
            yield last

答案 15 :(得分:0)

def check_prime(x):
    if (x < 2): 
       return 0
    elif (x == 2): 
       return 1
    t = range(x)
    for i in t[2:]:
       if (x % i == 0):
            return 0
    return 1

答案 16 :(得分:0)

您可以使用列表推导以相当优雅的方式创建素数列表。取自here:

>>> noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
>>> primes = [x for x in range(2, 50) if x not in noprimes]
>>> print primes
>>> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

答案 17 :(得分:0)

SymPy是一个用于符号数学的Python库。它提供了几个生成素数的函数。

getClasses()

以下是一些例子。

isprime(n)              # Test if n is a prime number (True) or not (False).

primerange(a, b)        # Generate a list of all prime numbers in the range [a, b).
randprime(a, b)         # Return a random prime number in the range [a, b).
primepi(n)              # Return the number of prime numbers less than or equal to n.

prime(nth)              # Return the nth prime, with the primes indexed as prime(1) = 2. The nth prime is approximately n*log(n) and can never be larger than 2**n.
prevprime(n, ith=1)     # Return the largest prime smaller than n
nextprime(n)            # Return the ith prime greater than n

sieve.primerange(a, b)  # Generate all prime numbers in the range [a, b), implemented as a dynamically growing sieve of Eratosthenes. 

答案 18 :(得分:0)

如果你想找到范围内的所有素数,你可以这样做:

def is_prime(num):
"""Returns True if the number is prime
else False."""
if num == 0 or num == 1:
    return False
for x in range(2, num):
    if num % x == 0:
        return False
else:
    return True
num = 0
itr = 0
tot = ''
while itr <= 100:
    itr = itr + 1
    num = num + 1
    if is_prime(num) == True:
        print(num)
        tot = tot + ' ' + str(num)
print(tot)

只需添加while its <=和您的号码即可 OUTPUT:
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

答案 19 :(得分:0)

这里是麻木的Eratosthenes筛子,具有很好的复杂性(比对长度为n的数组排序要低)和矢量化。

import numpy as np 
def generate_primes(n):
    is_prime = np.ones(n+1,dtype=bool)
    is_prime[0:2] = False
    for i in range(int(n**0.5)+1):
        if is_prime[i]:
            is_prime[i*2::i]=False
    return np.where(is_prime)[0]

时间:

import time    
for i in range(2,10):
    timer =time.time()
    generate_primes(10**i)
    print('n = 10^',i,' time =', round(time.time()-timer,6))

>> n = 10^ 2  time = 5.6e-05
>> n = 10^ 3  time = 6.4e-05
>> n = 10^ 4  time = 0.000114
>> n = 10^ 5  time = 0.000593
>> n = 10^ 6  time = 0.00467
>> n = 10^ 7  time = 0.177758
>> n = 10^ 8  time = 1.701312
>> n = 10^ 9  time = 19.322478

答案 20 :(得分:0)

使用生成器:

def primes(num):
    if 2 <= num:
        yield 2
    for i in range(3, num + 1, 2):
        if all(i % x != 0 for x in range(3, int(math.sqrt(i) + 1))):
            yield i

用法:

for i in primes(10):
    print(i)
  

2、3、5、7

答案 21 :(得分:0)

这就是我所拥有的:

def is_prime(num):
    if num < 2:         return False
    elif num < 4:       return True
    elif not num % 2:   return False
    elif num < 9:       return True
    elif not num % 3:   return False
    else:
        for n in range(5, int(math.sqrt(num) + 1), 6):
            if not num % n:
                return False
            elif not num % (n + 2):
                return False

    return True

大数字的速度非常快,因为它只检查数字除数的已经素数。

现在,如果要生成素数列表,可以执行以下操作:

# primes up to 'max'
def primes_max(max):
    yield 2
    for n in range(3, max, 2):
        if is_prime(n):
            yield n

# the first 'count' primes
def primes_count(count):
    counter = 0
    num = 3

    yield 2

    while counter < count:
        if is_prime(num):
            yield num
            counter += 1
        num += 2

在这里使用发电机可能需要提高效率。

仅供参考,而不是说:

one = 1
while one == 1:
    # do stuff
你可以简单地说:

while 1:
    #do stuff

答案 22 :(得分:0)

您需要确保所有可能的除数不均匀地划分您正在检查的数字。在这种情况下,只要其中一个可能的除数不能均匀地划分数字,你就会打印出你正在检查的数字。

此外,您不想使用continue语句,因为当您已经发现该数字不是素数时,继续只会使它检查下一个可能的除数。

答案 23 :(得分:0)

  • continue语句看起来不对。

  • 你想从2开始,因为2是第一个素数。

  • 你可以写“while True:”来获得无限循环。

答案 24 :(得分:-1)

import time

maxnum=input("You want the prime number of 1 through....")

n=2
prime=[]
start=time.time()

while n<=maxnum:

    d=2.0
    pr=True
    cntr=0

    while d<n**.5:

        if n%d==0:
            pr=False
        else:
            break
        d=d+1

    if cntr==0:

        prime.append(n)
        #print n

    n=n+1

print "Total time:",time.time()-start

答案 25 :(得分:-1)

对我来说,以下解决方案看起来简单易懂。

import math

def is_prime(num):

    if num < 2:
        return False

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

return True