Simple prime number generator in Python

前端 未结 26 1746
感情败类
感情败类 2020-11-22 07:16

Could someone please tell me what I\'m doing wrong with this code? It is just printing \'count\' anyway. I just want a very simple prime generator (nothing fancy).

相关标签:
26条回答
  • 2020-11-22 07:53

    You can create a list of primes using list comprehensions in a fairly elegant manner. Taken from 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]
    
    0 讨论(0)
  • 2020-11-22 07:54

    This is my implementation. Im sure there is a more efficient way, but seems to work. Basic flag use.

    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))
    
    0 讨论(0)
  • 2020-11-22 07:54

    How about this if you want to compute the prime directly:

    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
    
    0 讨论(0)
  • 2020-11-22 07:54
    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
    
    0 讨论(0)
  • 2020-11-22 07:55

    Here is what I have:

    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
    

    It's pretty fast for large numbers, as it only checks against already prime numbers for divisors of a number.

    Now if you want to generate a list of primes, you can do:

    # 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
    

    using generators here might be desired for efficiency.

    And just for reference, instead of saying:

    one = 1
    while one == 1:
        # do stuff
    

    you can simply say:

    while 1:
        #do stuff
    
    0 讨论(0)
  • 2020-11-22 07:57
    def primes(n): # simple sieve of multiples 
       odds = range(3, n+1, 2)
       sieve = set(sum([list(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]
    

    To test if a number is prime:

    >>> 541 in primes(541)
    True
    >>> 543 in primes(543)
    False
    
    0 讨论(0)
提交回复
热议问题