In this tutorial, we will explore how to create a Python program to print the first N prime numbers. This program is essential if you encounter questions like, “How can you print 10 prime numbers in Python?” or “How do you print the first 7 prime numbers in Python?” Knowing how to write a Python program to print the first N prime numbers is crucial, as it provides a flexible solution for printing any desired number of prime numbers.
Example:
N = 5
Output should be:
2
3
5
7
11
According to the definition, a Prime number is a natural number greater than 1 (one), that has only two factors, 1 and itself. In other words, a positive integer, except for 1 (one), that is divisible only by 1 and itself is called a prime number. Two (2) is the only even prime number. Other even numbers are divisible by 2 or other even numbers, which is why they are not considered prime numbers.
Program 1: Python program to print the first N prime numbers using while and for loop
# Python program to print first N prime numbers
from math import isqrt
N = int(input("Please enter a positive number: "))
# count variable is used to count total printed prime number
count = 0
num = 2
# iterating while loop until count is less than N
while count < N:
is_prime = True
# iterating foor loop to check number is prime or not
for i in range(2, isqrt(num) + 1):
if num % i == 0:
is_prime = False
break
# if is_prime True then number will be prime
if is_prime:
print(num)
count += 1
# increase num by 1 to check next number is prime or not
num += 1
Output
Please enter a positive number: 10
2
3
5
7
11
13
17
19
23
29
Explanation:
Why do we utilize the square root of ‘num’ when iterating through the ‘for’ loop?
The main fact is that a composite number must have a factor less than or equal to the square root of that number. Otherwise, the number is not a prime number.
- The program begins by prompting the user to enter a positive number (N) using
input()
. The entered value is then converted to an integer usingint()
. - Two variables
count
and num are initialized.count
is used to track the total number of printed prime numbers.num
starts from 2, the first prime number. - A
while
loop is used to iterate until the count of printed prime numbers (count
) is less than the user-specified number (N
). - Inside the loop, a boolean variable
is_prime
is initialized asTrue
for each iteration. - A
for
loop is used to check whether the current number (num
) is prime. It iterates from 2 to the square root ofnum
(usingisqrt
for efficiency). - If,
is_prime
isTrue
, indicating that the currentnum
is a prime number, it is printed. Thecount
is then incremented. - Regardless of whether the current number is prime or not,
num
is incremented by 1 to check the next number in the next iteration. - The program continues iterating through the while loop until the specified number of prime numbers (
N
) is printed.
Program 2: Python program to print the first N prime numbers using the function
# Python program to print first N prime numbers using function
from math import isqrt
def is_prime_number(num):
# check whether input number is less than 1 or not
if num <= 1:
return False
# Check for factors from 2 to the square root of the number + 1
for i in range(2, isqrt(num) + 1):
# if number is divisble by any number from the range then it is not prime number
if num % i == 0:
return False
return True
def print_n_prime_numbers(n):
# count variable is used to count total printed prime number
count = 0
num = 2
# iterating while loop until count is less than n
while count < n:
if is_prime_number(num):
print(num)
count += 1
num += 1
# calling function to print prime number by taking input from the user for number of prime numbers.
N = int(input("Please enter a positive number: "))
print_n_prime_numbers(N)
Output
Please enter a positive number: 5
2
3
5
7
11
Explanation:
- The program starts by prompting the user to input a positive number (N) using the
input()
function. The user is expected to enter the desired count of prime numbers they want to be printed. - The
print_n_prime_numbers
function is then called with the user-input value (N
) as an argument. This function is responsible for printing the first N prime numbers. - Inside the
print_n_prime_numbers
function, the provided value ofN
is used to determine how many prime numbers should be printed. - The function initializes variables
count
andnum
to keep track of the total printed prime numbers and the current number being considered. - It uses a
while
loop to continue the process until the count of printed prime numbers is less than N. - Inside the loop, the
is_prime_number
function is called to check whether the currentnum
is a prime number. - If it is prime, the number is printed, and the count is incremented.
- The program prints the first N prime numbers based on the user’s input.
- The user gets a list of prime numbers according to their specified count (N).
You should also read: