Python Program to create a list of first N prime number

In this post, we will learn how to create a list of the first N prime numbers. value of N is given by the user.

Implementation:

def nextPrime (num):
    while True:
        num+=1
        for i in range(2,num):
            if num%i==0:
                break
        else:
            return num

def primeProducer(N):
    num=1
    count=1
    while count<=N:
        num=nextPrime(num)
        yield num
        count+=1
N=int(input("How many prime number you want to generate?"))
l=[x for x in primeProducer(N)]
print(l)
Output:
How many prime number you want to generate?10
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Happy Coding!

Follow us on Instagram @programmersdoor

Join us on Telegram @programmersdoor


Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.

Follow Programmers Door for more.

#blog #interview #placement #learn #computer #science

        Contact Us

programmersdoor@gmail.com

  • LinkedIn
  • Facebook
  • Instagram

©2023 by Programmers Door