Table of Contents
TogglePython Program for Prime Number: A Beginner’s Guide
We all know about Prime Number. So basically they are special numbers greater than 1 that has only 2 divisors: 1 and itself. This means it cannot be divided evenly by any other numbers.
For Example – Prime Numbers are 2, 3, 5, 7, and 11.
Prime numbers are important in many areas, like cryptography (securing data) and mathematics theory.
In this blog, I will go through a Python program to check for prime numbers. We will also explore different methods to create a prime number program in Python.
Enroll now and take the first step towards a successful career. Click here to join our Python Programming course today!
What is a Prime Number?
As we discussed earlier a prime number is only divisible by 1 and itself without leaving any remainder.
Examples of prime numbers include 2, 3, 5, 7, 11, and 13.
Why are Prime Numbers Important?
Prime numbers are the basics of number theory. They are used a lot in cryptography, computer algorithms, and hashing functions. Knowing how to check if a number is prime is useful for developers working with algorithms or encryption.
Also Read – How long Does it take to learn Python
Methods for Python Program to Check Prime Numbers
1. Checking Prime Numbers Using Recursion
A recursive function can check if a number is divisible by any number less than it.
We start from a divisor and decrement until 1, returning False if a divisor divides the number without leaving a remainder.
Code –
Output: True
Output: False
Basic logic of this code –
Step 1 – Start with divisor = n – 1 if no divisor is provided.
Step 2 – Non-Prime Check: If n≤1n \leq 1n≤1, return False (not prime).
Step 3 – Prime Confirmation: If divisor reaches 1, return True (no divisors found, so it’s prime).
Step 4 – Divisibility Check: If n is divisible by divisor, return False (has a factor, so not prime).
Step 5 – Recursive Call: If not divisible, call the function again with divisor – 1.
Also Read – What is Python Programming Language
2. Checking Prime Numbers Using a For Loop
Basic Logic:
- Handle Small Numbers: If n≤1n \leq 1n≤1, return False because numbers less than 2 are not prime.
- Loop Through Potential Divisors: Use a for loop to check divisibility starting from 2 up to n\sqrt{n}n (since any factor larger than n\sqrt{n}n would already have a smaller pair factor).
- Divisibility Check: If n is divisible by any number in this range, return False (not prime).
- No Divisors Found: If the loop completes without finding a divisor, return True (it’s prime).
Code –
Output: True
Output: False
3. Checking Prime Numbers Using a While Loop
Let’s write a simple Python program to check if a given number is prime. This will involve using a function and a loop to verify that the number is only divisible by 1 and itself.
Basic Logic:
Function is_prime(number): Checks if a number is prime.
Rule for Small Numbers: If the number is 1 or less, it’s not prime.
Check Divisibility: For numbers greater than 1, check if they’re divisible by any number from 2 up to the square root of the number. If it is, the number isn’t prime.
Result: If no divisors are found, the number is prime.
User Input and Output: The code then takes a number from the user, checks if it’s prime, and displays the result.
Code –
Explanation of the Code
- Define the Function: The function is_prime(number) checks if a number is prime.
- Handle Edge Cases: If the number is less than or equal to 1, it is not prime.
- Loop to Check Divisibility: We loop from 2 up to the square root of the number (using int(number ** 0.5) + 1) to check if any number divides number without leaving a remainder.
- If it finds any such divisor, the function returns False, indicating the number is not prime.
- Return True if Prime: If no divisors are found, the function returns True, meaning the number is prime.
How to Test the Program
- Example 1:
- Input: num = 5
- Output: 5 is a prime number.
- Example 2:
- Input: num = 8
- Output: 8 is not a prime number.
- Edge Case:
- Input: num = 1
- Output: 1 is not a prime number.
Also Read – Basic Python Programs
Optimizing the Prime Check
To make the program faster, we only need to check up to the square root of a number to see if it’s divisible by any smaller numbers.
Why? Because if a larger number divides evenly, there would already be a smaller factor that we found below the square root. This saves time and makes the program run faster, especially for big numbers.
Also Read – Python linear search
Conclusion
I hope you understand the Python Program for Prime Numbers.
- Prime numbers are important in mathematics & programming as well.
- This Python program helps you check if a number is a prime number or not.
- It’s a good and simple way for beginners to practice using loops, if-else statements, and functions.
- Once you understand this, you can try more advanced tasks, like:
- Creating lists of prime numbers
- Using primes in bigger algorithms
If you are Fresher and want to start a career in Programming?
Enroll Technogeeks Python Programming Course and Contact Us for more information.