To find first N prime numbers in python

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

    The line k = k-1 does not do what you think. It has no effect. Changing k does not affect the loop. At each iteration, k is assigned to the next element of the range, so any changes you have made to k inside the loop will be overwritten.

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

    This is my version

    import timeit
    import math
    
    __author__ = 'rain'
    
    
    primes = [2]
    
    def is_prime(n):
        for prime in primes:
            if n % prime == 0:
                return False
        return True
    
    
    def find_nth_prime(n):
        current_index = 0
        while(len(primes) < n):
            if current_index == 0:
                start_value = 3
                end_value = 2 * 2
            else:
                start_value = primes[current_index - 1] * primes[current_index - 1] + 1
                end_value = primes[current_index] * primes[current_index]
            for i in range(start_value, end_value):
                if is_prime(i):
                    primes.append(i)
            current_index += 1
        return primes[n-1]
    
    
    def solve():
        return find_nth_prime(10001)
    
    print solve()
    
    print timeit.timeit(solve, number=10)
    

    I use a sieve to scan primes, it's quite quick

    It's take only 3.8e-06 seconds to get 10001th prime (10 times).

    0 讨论(0)
  • 2020-11-28 08:01
    def Isprime(z):
        '''returns True if the number is prime OTW returns false'''
        if z<1:
            return False
        elif z==1:
            return False
    
        elif z==2:
            return True 
    
        else:
            for i in range(2,z):
                if z%i==0:
                    return False
                else:
                    return True
    

    This is the way I did it. Of course, there are so many ways you can do it.

    0 讨论(0)
  • 2020-11-28 08:02
    prime=2
    counter = 0
    x = int(input("Enter the number:\n"))
    while (counter < x):
        if all(prime%j!=0 for j in range(2, prime)):
            print(prime, "is a prime number")
            counter+=1
    
    
        prime+=1
    
    0 讨论(0)
  • 2020-11-28 08:02
    def isprime(n):
        if n <= 1:
            return False
        for x in range(2, n):
            if n % x == 0:
                return False
        else:
            return True
    
    def list_prime(z):
        y = 0
        def to_infinity():
            index=0
            while 1:
                yield index
                index += 1
        for n in to_infinity():
            if y < z:
                if isprime(n):
                    y = y + 1
                    print(n, end='\n', flush=True)
            else:break
        print(f'\n {z} prime numbers are as above.')
    
    # put your range below
    list_prime(10)
    
    0 讨论(0)
提交回复
热议问题