How to Compute the Prime Number in Python 3?
A number is called prime if it is divisible by one and itself. If it is divisible by more it is called Composite Number. But there is an exception that the number 1 is neither prime nor the composite number. It is one important constraint that you have to handle while computing for prime number.How did I compute it?
Let me write pseudo code for a prime number so that you can get the idea about how to compute for prime number.Algorithm prime(n):
if n == 1:
return 'Not a prime'
for(d=2,d<n/2;d++):
if n%d == 0:
return 'Not a prime'
return "It is prime"
Here the main concept is we are dividing iteratively the given number by 2 till it reaches half of the given number(n/2). Since it cannot be divisible by any number beyond n/2 as it is remainder only. In between, if it gets divided by any of the numbers than it is not a prime number or else it is a prime number.
The source code to compute the prime number in python 3 is given below. If you want to do in python 2 the only difference is the use of the print function. Just remove the parentheses from print function.