If a quantity p is totally divisible by one other quantity q, then q is claimed to be an element of p. Right here q can be larger than 1 and fewer than or equal to p.
So, if a quantity p is totally divisible by one other quantity q and q is a primary quantity then q is claimed to be a primary issue of p.
Prime numbers are these numbers which might be solely divisible by 1 and themselves. On this article, we are going to see the code to seek out the prime components of a quantity.
For instance:
If we take 5, then prime components can be:
5 = 5
5 is the one prime issue of 5.
If we take 20, then prime components can be:
20 = 2 * 2 * 5
If we take 210, then prime components can be:
210 = 2 * 3 * 5 * 7
Now let’s see the code of this drawback. On this code, we can be creating two features. The primary operate will assist us to know if a quantity is prime or not. And the second operate will assist us to print the prime issue of a quantity.
# If the Quantity is lower than 2, then returns False
if n < 2:
return False
# loop from 2 to n-1, if between this vary n is split by any quantity
# then return False
# else return true ultimately.
for i in vary(2, n):
if (n % i == 0):
return False
return True
# Operate to print the prime components of a quantity.
def primeFactor(n):
“””
Operate to print the prime components of a quantity.
Enter : Quantity
Output : None. It is going to print the prime components from the operate solely.
“””
if (n < 2):
print(“No Prime Elements”)
return
i = 2
# Loop whereas i is just not equal to n
whereas (i < n+1):
# if n is divisible by i and that i is a primary quantity then we now have to
# print the vlaue of i and replace the worth of n by dividing it by i.
# else we simply must increment the worth of i by 1.
if (n % i == 0 and isPrime(i)):
print(i) # print the worth of i
n = n / i # replace the worth of n
else:
i += 1 # Improve the worth of i by 1
# Take the quantity as enter from consumer within the string format
quantity = enter(“Enter the Quantity : “)
# Convert the string datatype into integer datatype
quantity = int(quantity)
# Name the operate to print the prime components of the inputted quantity
print(“Prime Issue : “)
primeFactor(quantity)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
def isPrime(n): “”“ Operate to return if a quantity is prime or not Enter : A Quantity Output : Boolean Worth True or False ““”
# If the Quantity is lower than 2, then returns False if n < 2: return False
# loop from 2 to n-1, if between this vary n is split by any quantity # then return False # else return true ultimately. for i in vary(2, n): if (n % i == 0): return False
return True
# Operate to print the prime components of a quantity. def primeFactor(n): “”“ Operate to print the prime components of a quantity. Enter : Quantity Output : None. It is going to print the prime components from the operate solely.
““” if (n < 2): print(“No Prime Elements”) return
i = 2 # Loop whereas i is just not equal to n whereas (i < n+1): # if n is divisible by i and that i is a primary quantity then we now have to # print the vlaue of i and replace the worth of n by dividing it by i. # else we simply must increment the worth of i by 1. if (n % i == 0 and isPrime(i)): print(i) # print the worth of i n = n / i # replace the worth of n else: i += 1 # Improve the worth of i by 1
# Take the quantity as enter from consumer within the string format quantity = enter(“Enter the Quantity : “)
# Convert the string datatype into integer datatype quantity = int(quantity)
# Name the operate to print the prime components of the inputted quantity print(“Prime Issue : “) primeFactor(quantity)
|
Testcase 1: When the enter is 100.
Output:
PS C : Customers ASUS Desktop Loopy Programmer Work > python –u ” c : Customers ASUS Desktop Loopy Programmer Work take a look at.py “ Enter the Quantity : 100 Prime Issue : 2 2 5 5
|
Testcase 2: When the Enter is 270270.
Output:
PS C : Customers ASUS Desktop Loopy Programmer Work > python –u ” c : Customers ASUS Desktop Loopy Programmer Work take a look at.py “ Enter the Quantity : 270270 Prime Issue : 2 3 3 3 5 7 11 13
|
Now we have used two features to create the entire program.
The primary operate will inform us if a quantity is prime or not. It takes an integer and returns a Boolean worth (True or false).
The second operate will assist to print the prime components of a quantity. It is going to take an integer as enter and print all of the prime components of that enter. It returns None as a result of all of the printing is finished throughout the operate solely.