Prime number program in python
1.Prime number:
What is meant by prime number ?
_It is positive Integer which is Only divisible by itself and one
2.Composite Number:
If a integer is composite in smaller number it called as composite number
Type = 1:
Write a Prime number program in python?
Write a Prime number program in python?
_When Number is known to programmer e.g. n=13
n =13
for a in range (2,n): //It will not include n i.e. 13 and 1//
if n % a==0:
print("Not a prime number")
break
else:
print("Prime")
output = prime
We can change number 'n' lets take composite number n=6
n =6
for a in range (2,n) : //It will not include n i.e. 13 and 1//
if n % a==0:
print("Not a prime number")
break
else:
print("Prime")
code:
x =input("Enter a number")
a=int(x)
for b in range(2,a):
if a % b==0:
print ("Not a prime number")
break
else:
print("Prime Number")
All the program that we have discuss above are valid for one number or less numbers but when we deal with thousands of number then above method is not efficient as we consider time to check prime number it check for all numbers between 2 to that number itself and if number is in lakh or in crore this method will become insignificant
There are Two more algorithm to check prime number or not that we will discuss . And time required to run them is much less as compare to above one
Type = 2:
from math import *
x=input("Enter a number")
a=int(x)
if a==1:
print("Unit number")
max_divisior= floor(sqrt(a))
for b in range(2,1 + max_divisior):
if a%b==0:
print("Not a prime number")
break
else:
print("Prime number")
break
In above method we check for divisor take a example
16 and its divisors
= 16*1
8*2
4*4 "Product of under root of 16 i.e. the given number" √16 ✕√16
2*8
1*162*8
so if you observe the above list the terms after (√16 ✕√16 )" Product of under root of 16 i.e. the given number is same below the term(√16 ✕√16) "Product of under root of 16 i.e. the given number"
it means Under root of that number provides a imaginary line and we can check only half of divisor
for prime number
In above method time required to execute the code is very less as compare to type 1
we can check for larger number as well
Type 3:
Type 3 is the most efficient CODE , when we are dealing with time as a parameter
In above code we check for all even integer over a limit ,this is waste of time if the input is even and greater than two it can not be a prime Number ,And if input is odd it is waste to check for
even divisors we overcome from this drawbacks in our Type 3 method
Where if a number is even and greater than two we directly print("Not a prime number") and if input is odd then we check divisor only for odd numbers.
To check for large range of numbers, we have to define a function and we have to use one inbuild time function ,to check time required for execution of code . we define a function as def is_prime():
code =
from math import * # '* ' simply means import all function from mathimport time
def is_prime(a):
if a <= 1:
return False
if a == 2:
return True
if a > 2 and a % 2 == 0:
return False
max_divisor = floor(sqrt(a))
for b in range(3, 1 + max_divisor, 2):
if a % b == 0:
return False
return True
# Time functiont0 = time.time()
for a in range(1, 100000):
is_prime(a)
t1 = time.time()
print("Time required :", t1 - t0)
time required ≅0.34 sec
You can check time required for above two method by converting them into def is_prime():
function
time required for first method is ≅ 55 sec
time required for second method is ≅ 0.59 sec
We will discuss in detail about how to define a function and how to use time function in our next article
I hope this article on "Prime Number" in python is helpful for u
Thank You 😊