Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Prime Number Program in Python (Check Prime or Not)
Numbers have always fascinated humanity, and among them, prime numbers stand out as captivating mathematical entities.
Prime numbers are integers greater than 1 that possess a unique quality – they can only be divided evenly by 1 and themselves, with no other factors in between.
The ways to identify prime numbers has been interesting for mathematicians and programmers alike, leading to the development of efficient algorithms to tackle this timeless challenge.
Here, we will explore the different approaches on how to find prime numbers in Python. We will showcase a variety of programs, providing clear examples, output, and detailed code explanations to simplify the process.
By going through these Python program examples, you will gain a deeper understanding of number divisibility, loops, conditionals, and other fundamental programming concepts in Python. Moreover, you will explore the strengths and weaknesses of different prime checking algorithms, enabling you to make informed decisions when selecting the most suitable approach for your specific requirements.
Prime Number Logic in Python
The logic to check if a number is prime in Python generally involves iterating from 2 to the square root of the number (inclusive) and checking if the number is divisible by any of those values. If it is divisible by any value, then it is not a prime number. Otherwise, it is a prime number.
Here's a step-by-step explanation of the prime number logic in Python:
-
Start with a number. Let's call it num.
-
Check if num is less than or equal to 1. If so, it is not a prime number. Return False.
-
Iterate through numbers from 2 to the square root of num (inclusive). You can use the range function with the appropriate arguments.
-
For each number i in the range, check if num is divisible by i. If num is divisible by i without a remainder, it means num is not a prime number. Return False.
-
If the loop completes without finding any factors, it means num is a prime number. Return True.
Let’s dive into the Prime number program in Python!
Prime Number Program in Python
Code
# Simple prime number program in Python using function and for loop
def is_prime(number):
if number <= 1:
return False
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False
return True
# Example usage
num = int(input("Enter a number: "))
if is_prime(num):
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")
Output
Enter a number: 37
37 is a prime number.
Explanation
Here, we defined the is_prime function that takes a number as user input and returns True if it is prime and False otherwise.
To check if a number is prime or not in Python, we follow these steps:
-
If the number is less than or equal to 1, we immediately return False, as prime numbers are greater than 1.
-
We use a for loop to iterate from 2 to the square root of the given number (inclusive). We only need to check divisibility up to the square root of the number, as any factors beyond that would already have corresponding factors within the range we check.
-
For each value of i in the loop, we check if the number is divisible by i. If it is, we return False, indicating that the number is not prime.
-
If the loop completes without finding any factors, we return True, indicating that the number is prime.
In the example usage section, we prompt the user to enter a number using the input function, convert it to an integer using the int function, and store it in the variable num.
We then call the is_prime function with num as an argument to determine if the number is prime or not. Finally, we display an appropriate message based on the result.
Find Prime Number in Python With User Input
Code
# Python program to check prime number with user input
def is_prime(number):
if number <= 1:
return False
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False
return True
# Prompt the user to enter a number
num = int(input("Enter a number: "))
if is_prime(num):
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")
Output
Enter a number: 34
34 is not a prime number.
Explanation
- In this program, we define a function called is_prime that takes a number as input and returns True if it is prime and False otherwise. The implementation of the is_prime function is the same as in the previous examples.
-
We prompt the user to enter a number using the input function, convert it to an integer using the int function, and store it in the variable num.
-
Next, we call the is_prime function with num as an argument to determine if the number is prime or not. Finally, we display an appropriate message based on the result.
-
When you run this program, it will prompt you to enter a number. After you provide the input, it will determine if the number is prime or not and display the result.
Try running this program using the online Python compiler, with different numbers to check if they are prime or not.
Prime Number Program in Python Using while loop
Code
# How to find prime numbers in Python using while loop
def is_prime(number):
if number <= 1:
return False
i = 2
while i < number:
if number % i == 0:
return False
i += 1
return True
# Example usage
num = int(input("Enter a number: "))
if is_prime(num):
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")
Output
Enter a number: 71
71 is a prime number.
Explanation
We define a function called is_prime that takes a number as input and returns True if it is prime and False otherwise.
To determine if a number is prime or not in Python programming, we follow these steps:
-
If the number is less than or equal to 1, we immediately return False, as prime numbers are greater than 1.
-
We initialize i as 2 and start a while loop that continues until i is less than the given number.
-
Inside the loop, we check if the number is divisible by i. If it is, we return False, indicating that the number is not prime.
-
If the loop completes without finding any factors, we return True, indicating that the number is prime.
-
In the example usage section, we prompt the user to enter a number using the input function, convert it to an integer using the int function, and store it in the variable num.
-
We then call the is_prime function with num as an argument to determine if the number is prime or not. Finally, we display an appropriate message based on the result.
Python Program for Prime Number Using Recursion
Here's an example of a Python program that checks if a number is prime using recursion:
Code
# Check prime number or not using recursion in Python
def is_prime(number, divisor=2):
if number <= 1:
return False
if number == divisor:
return True
if number % divisor == 0:
return False
return is_prime(number, divisor + 1)
# Example usage
num = int(input("Enter a number: "))
if is_prime(num):
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")
Output
Enter a number: 7
7 is a prime number.
Explanation
In this program, we define a recursive function called is_prime that takes a number and a divisor as input. The function checks if the number is prime by recursively dividing it by numbers starting from the given divisor.
The steps involved in the recursive function are as follows:
-
If the number is less than or equal to 1, we immediately return False, as prime numbers are greater than 1.
-
If the number is equal to the current divisor, it means that we have checked all possible divisors without finding any factors. In this case, we return True, indicating that the number is prime.
-
If the number is divisible by the current divisor without a remainder, we return False, indicating that the number is not prime.
-
Otherwise, we recursively call the is_prime function, incrementing the divisor by 1 in each recursive call.
-
In the example usage section, we prompt the user to enter a number using the input function, convert it to an integer using the int function, and store it in the variable num.
We then call the is_prime function with num as an argument to determine if the number is prime or not. Finally, we display an appropriate message based on the result.
Python Program to Check Prime Number Between 1 to 100
Code
# Python program to check prime number in given range of 1 to 100
def is_prime(number):
if number <= 1:
return False
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False
return True
# Iterate through numbers from 1 to 100
for num in range(1, 101):
if is_prime(num):
print(num, end=' ')
Output
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Explanation
In this program, we use the is_prime function.
We then iterate through the numbers from 1 to 100 using a for loop. For each number, we call the is_prime function to determine if it is prime. If it is, we print the number on the same line, separated by a space, using the end=' ' parameter in the print function.
When you run this program, it will iterate through the numbers from 1 to 100 and print the prime numbers on a single line, separated by spaces.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program