Simple prime number generator in Python

前端 未结 26 1779
感情败类
感情败类 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:57

    This seems homework-y, so I'll give a hint rather than a detailed explanation. Correct me if I've assumed wrong.

    You're doing fine as far as bailing out when you see an even divisor.

    But you're printing 'count' as soon as you see even one number that doesn't divide into it. 2, for instance, does not divide evenly into 9. But that doesn't make 9 a prime. You might want to keep going until you're sure no number in the range matches.

    (as others have replied, a Sieve is a much more efficient way to go... just trying to help you understand why this specific code isn't doing what you want)

    0 讨论(0)
  • 2020-11-22 07:58

    Similar to user107745, but using 'all' instead of double negation (a little bit more readable, but I think same performance):

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

    Basically it iterates over the x in range of (2, 100) and picking only those that do not have mod == 0 for all t in range(2,x)

    Another way is probably just populating the prime numbers as we go:

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

    We will get all the prime numbers upto 20 in a list. I could have used Sieve of Eratosthenes but you said you want something very simple. ;)

    0 讨论(0)
  • 2020-11-22 08:01

    Using generator:

    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
    

    Usage:

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

    2, 3, 5, 7

    0 讨论(0)
  • 2020-11-22 08:02
    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
    
    0 讨论(0)
  • 2020-11-22 08:03

    If you wanted to find all the primes in a range you could do this:

    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)
    

    Just add while its <= and your number for the range.
    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

    0 讨论(0)
提交回复
热议问题