To find first N prime numbers in python

前端 未结 29 1947
醉梦人生
醉梦人生 2020-11-28 06:56

I am new to the programming world. I was just writing this code in python to generate N prime numbers. User should input the value for N which is the total number of prime n

相关标签:
29条回答
  • 2020-11-28 07:53

    Using generator expressions to create a sequence of all primes and slice the 100th out of that.

    from itertools import count, islice
    primes = (n for n in count(2) if all(n % d for d in range(2, n)))
    print("100th prime is %d" % next(islice(primes, 99, 100)))
    
    0 讨论(0)
  • 2020-11-28 07:53

    You can take the number of prime number inputs. As per your method I have taken here a predefined count of 10:

    i = 2
    if i == 2:
        print(str(i) + "is a prime no")
        i = i+1
    c=1
    
    while c<10:
        for j in range(2, i):
            if i%j==0:
                break
    
        if i == j+1:
            print(str(i) + "is aa prime no")
            c=c+1
    
        i=i+1
    
    0 讨论(0)
  • 2020-11-28 07:54

    For reference, there's a pretty significant speed difference between the various stated solutions. Here is some comparison code. The solution pointed to by Lennart is called "historic", the one proposed by Ants is called "naive", and the one by RC is called "regexp."

    from sys import argv
    from time import time
    
    def prime(i, primes):
        for prime in primes:
            if not (i == prime or i % prime):
                return False
        primes.add(i)
        return i
    
    def historic(n):
        primes = set([2])
        i, p = 2, 0
        while True:
            if prime(i, primes):
                p += 1
                if p == n:
                    return primes
            i += 1
    
    def naive(n):
        from itertools import count, islice
        primes = (n for n in count(2) if all(n % d for d in range(2, n)))
        return islice(primes, 0, n)
    
    def isPrime(n):
        import re
        # see http://tinyurl.com/3dbhjv
        return re.match(r'^1?$|^(11+?)\1+$', '1' * n) == None
    
    def regexp(n):
        import sys
        N = int(sys.argv[1]) # number of primes wanted (from command-line)
        M = 100              # upper-bound of search space
        l = list()           # result list
    
        while len(l) < N:
            l += filter(isPrime, range(M - 100, M)) # append prime element of [M - 100, M] to l
            M += 100                                # increment upper-bound
    
        return l[:N] # print result list limited to N elements
    
    def dotime(func, n):
        print func.__name__
        start = time()
        print sorted(list(func(n)))
        print 'Time in seconds: ' + str(time() - start)
    
    if __name__ == "__main__":
        for func in naive, historic, regexp:
            dotime(func, int(argv[1]))
    

    The output of this on my machine for n = 100 is:

    naive
    [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]
    Time in seconds: 0.0219371318817
    historic
    [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]
    Time in seconds: 0.00515413284302
    regexp
    [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]
    Time in seconds: 0.0733318328857
    

    As you can see, there's a pretty big discrepancy. Here it is again for 1000 (prime outputs removed):

    naive
    Time in seconds: 1.49018788338
    historic
    Time in seconds: 0.148319005966
    regexp
    Time in seconds: 29.2350409031
    
    0 讨论(0)
  • 2020-11-28 07:57

    Anwer here is simple i.e run the loop 'n' times.

    n=int(input())
    count=0
    i=2
    while count<n:
        flag=0
        j=2
        while j<=int(i**0.5):
            if i%j==0:
                flag+=1
            j+=1
        if flag==0:
            print(i,end=" ")
            count+=1
        i+=1
    
    0 讨论(0)
  • 2020-11-28 07:57

    Hi! I am very new to coding, just started 4 days back. I wrote a code to give back the first 1000 prime numbers including 1. Have a look

    n=1
    c=0
    while n>0:
       for i in range(2,n):
          if n%i == 0:
             break
       else:
          print(n,'is a prime')
          c=c+1
       n=n+1
       if c==1000:
          break
    
    0 讨论(0)
  • 2020-11-28 07:59

    using a regexp :)

    #!/usr/bin/python
    
    import re, sys
    
    
    def isPrime(n):
        # see http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/
        return re.match(r'^1?$|^(11+?)\1+$', '1' * n) == None
    
    
    N = int(sys.argv[1]) # number of primes wanted (from command-line)
    M = 100              # upper-bound of search space
    l = list()           # result list
    
    while len(l) < N:
        l += filter(isPrime, range(M - 100, M)) # append prime element of [M - 100, M] to l
        M += 100                                # increment upper-bound
    
    print l[:N] # print result list limited to N elements
    
    0 讨论(0)
提交回复
热议问题