Site icon Codeforcoding

Write a Python program to find the first n prime numbers

Write a Python program to find the first n prime numbers

In this article we are going to discuss the concept of “Write a Python program to find the first n prime numbers”

Prime numbers are fundamental in mathematics, appearing in numerous applications from cryptography to number theory. They are integers greater than 1, divisible only by 1, and themselves.

find the first n prime numbers

In this article, we’ll explore how to write a Python program to compute the first.

Program 1

# Write a Python program to find the first n prime numbers
n = int(input("Enter the number of prime numbers to find: "))

if n <= 0:
    print("Please enter a positive integer.")
    #input a positive integer from user
else:
    primes = []
    num = 2  # Start checking for primes from 2
    
    while len(primes) < n:
        is_prime = True
        for i in range(2, int(num**0.5) + 1):
            if num % i == 0:
                is_prime = False
                break
        if is_prime:
            primes.append(num)
        num += 1

    print(f"The first {n} prime numbers are: {primes}")

When the above code is executed, it produces the following result

Enter the number of prime numbers to find: 10
The first 10 prime numbers are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

 

Explanation:

Program 2

# Write a python program to find first n prime numbers
def is_prime(num):
    if num<2:
        return False
    for i in range(2, int(num**0.5) + 1):
        if num%i==0:
            return False
    return True
    
def first_n_primes(n):
    primes=[]
    num=2 #start cheking from 2 , the smallest prime
    while len(primes)<n:
        if is_prime(num):
            primes.append(num)
        num+=1
    return primes


#input the number opf primes to find
n=int(input("Enter the number to find prime numbers: "))
print(f"The first {n} prime numbers are: {first_n_primes(n)}")

When the above code is executed, it produces the following result

Enter the number to find prime numbers: 6
The first 6 prime numbers are: [2, 3, 5, 7, 11, 13]

 

 

Suggested for you

Program to find whether a Number is Prime or Not in C++

Program to find whether a Number is Prime or Not in C

Program to find whether a Number is Prime or Not in Java

Program to find whether a Number is Prime or Not in Python

Program to find whether a Number is Prime or Not in PHP

Program to find whether a Number is Prime or Not using the PHP function

 

C++ program to find first n prime numbers

Java program to find first n prime numbers

C program to find first n prime numbers

 

 

C code to 5 ways to check whether the given integer is Even or Odd

Java code to 5 ways to check whether the given integer is Even or Odd

C++ code to 5 ways to check whether the given integer is Even or Odd

Python code to 5 ways to check whether the given integer is Even or Odd

Csharp:Print all prime numbers between 1 to 100 using do-while loop
Exit mobile version