When I was a child and studying in school, our teacher taught us about prime numbers and composite numbers. But when I grew up and learned Python, I came across the queries of prime number program in Python. Then I learned and practised various ways to do it.
Finally, I found two different and best approaches to find the prime numbers in Python. Today, I am going to let you know those two methods one by one and step by step. To offer more clarity about the topic, I have mentioned the programs that you can use and implement to effectively understand the working of the methods.
So, let’s delve into today’s topic, but before that, let’s check what are prime and composite numbers?
What are the prime numbers?
If the natural number is higher than 1 and has no other positive divisors than 1, the number itself is known as a Prime number.
Prime numbers include 3, 7, 11, and so on.
What are the composite numbers?
Composite numbers are other natural numbers that are not prime numbers.
For example, composite numbers include 4, 6, 9, and so on.
How to check the prime number program in Python?
Using the flag variable:
# Program for checking whether a number is prime or not # To take an input value from the user x = int(input("Enter the input number: ")) # define the flag variable flag = False # define the prime numbers are greater than 1 if x > 1: # check for factors for i in range(2, x): if (x % i) == 0: # if factor is defined or found, set flag as True flag = True # break the condition of the loop break # check whether the flag is True if flag: print(x, "is a composite number (or not a prime number)") else: print(x, "is the prime number")
Enter the input number: 54
54 is a composite number (or not a prime number)
In the above program, you can check whether the number is prime or not. In this example, we must proceed if the num is higher than 1.
Check if x is divisible by the number from 2 to x -1. And also, find the factor within the range, then the number is considered to be prime. If the condition is true, then set the flag to True and break the loop.
But do not forget to check the flag outside the loop.
- Suppose the flag is true, then x will not be a prime number.
- In case the flag is false, then the x will be a prime number.
Note: To improve the program, you can decrease the number range to look at different factors. In the given program, we will search the range that is from 2 to x -1. You can also use the range, range(2, x//2) or range(2,math.floor(math.sqrt(x)+1)).
The range depends on the fact that the composite numbers must have a factor equal to or less than the square root of the particular number. Or it would be the prime number.
You can adjust the value of the variable x in the given source code. And check if the number is prime or not.
We can also use the for…else statement to find the prime number program in Python. This is possible without using an extra flag variable within Python.
|Check details on function in Python.|
Using a for…else statement:
# Program for checking if an x is prime or not # To take input from the user x = int(input("Enter an input number: ")) # prime numbers are higher than 1 if x > 1: # check for variables for i in range(2,x): if (x % i) == 0: print(x,"is a composite number (not a prime no)") print(i,"times",x//i,"is",x) break else: print(x,"is the prime number") # if the input value is less than or equal to 1, #it is a composite else: print(num,"is a composite (not a prime number)")
Enter an input number: 54
54 is a composite number (not a prime no)
2 times 27 is 54
In the above example, we use a for…else statement for finding the prime number program in Python. It works with the logic where the else clause that while run the for loop and break will exit from the for loop. This condition will work if there is no factor.
It means that the given number will be prime. Therefore, you can print the number as the prime in the else clause.
This is how you can easily use the prime number program in Python.
|Check details on Fibonacci series in Python.|
Check an alternative method or prime number program in Python
Python users can use the optimization method to find out the prime number. In this, rather than checking the number to n terms, you can simply check √n. The large factor of the n term will be multiplied by the smaller one.
Check the below program to understand how the actual optimised method works.
from math import sqrt # x is the number that we use to check whether #it is a prime or composite number x = 13 # now let's check from 2 to sqrt(n) # if it is found any factor then #the output will be as not a prime number # this flag defines the status #whether the x is prime or not prime_flag = 0 if(x > 1): for i in range(2, int(sqrt(x)) + 1): if (x % i == 0): prime_flag = 1 break if (prime_flag == 0): print("true") else: print("false") else: print("false")
The given algorithm can enhance by considering that the primes number must verify the form 6k ± 1 except 2 and 3. This will happen as the integers can easily express as (6k + i), where i = -1, 0, 1, 2, 3, and 4. (order ambien canada) Or we can say 2 will divide (6k + 0), (6k + 2), (6k + 4); and 3 will divide (6k + 3).
In a simple way, we can define that you can test whether the number is divided by 2 or 3, check the number with the form 6k ± 1.
Finding the prime number in any programming language is one of the basic tasks. That is why students and beginners of programming languages are always assigned to write prime number program in Python. If you are unable to do it, then use the above-mentioned methods. This will help you to check the prime as well as composite numbers.
Do you still have any queries regarding prime numbers in Python? Get in touch with our experts to get clarity with your doubts. And if you have any concerns with the above details, let me know through your mails.
Keep connected with CODEAVAIL and get the best guide from professionals.
Frequently Asked Questions
What is prime number in Python?
If the natural number is higher than 1 and has no other positive divisors than 1, the number itself is known as a Prime number. 2, 3, 5, 7, etc., are prime numbers because these numbers do not have another factor.
How do you find prime numbers in Python?
To find a prime number in Python, use a for loop to make the iteration the value from the beginning to finish and verify if each integer divides n if it is more than 1. If we find another number that divides, then you should print it.