To find first N prime numbers in python

前端 未结 29 2229
醉梦人生
醉梦人生 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:46

    This might help:

    import sys
    from time import time
    def prime(N):
        M=100
        l=[]
        while len(l) < N:
            for i in range(M-100,M):    
                num = filter(lambda y :i % y == 0,(y for y in range(2 ,(i/2)))) 
                if not num and i not in [0,1,4]:
                    l.append(i)
            M +=100
        return l[:N]
    
    
    def dotime(func, n):
        print func.__name__
        start = time()
        print sorted(list(func(n))),len(list(func(n)))
        print 'Time in seconds: ' + str(time() - start)
    
    
    if __name__ == "__main__":
        dotime(prime, int(sys.argv[1]))
    

提交回复
热议问题