Q:

How do you find the prime factor of a number in Python?

0

How do you find the prime factor of a number in Python

In this exercise, you will learn a Python program to print prime factors of a number. Before starting the coding part, it should be clear about the prime number and prime factor.

A prime number is a whole number greater than 1, whose only factors are 1 and itself. The prime numbers, which divide the given number perfectly, are known as the prime factors of that number. Basically, it is finding prime numbers that prime numbers multiply together to make the original number. The prime factorization serves useful when working with big numbers, such as in Cryptography or it is a commonly used in mathematical problem to secure public-key encryption systems. For example, if the input number is 110, then the output should be "2 5 11". And if the input number is 99, then the output should be "3 11". With the help of these two conditions, we can check whether a number is a prime factor of a given number or not-

  • The number must be a prime number.
  • The number must perfectly divide the given number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to find prime factors using for loop

Here is the program to find the prime factors of a number using Python. This program allows the user to enter any positive integer using the input() method. Next, Python returns the prime factors of that number using the nested for loop.

# Python Program to find Prime Factors of a Number
 
num = int(input(" Please enter any number: "))

for x in range(2, num + 1):
    if(num % x == 0):
        chk_prime = 1
        for y in range(2, (x //2 + 1)):
            if(x % y == 0):
                chk_prime = 0
                break
            
        if (chk_prime == 1):
            print(" %d is a Prime Factor of the given number %d" %(x, num))

Output of the above code -

Please enter any number: 99

3 is a Prime Factor of the given number 99

 11 is a Prime Factor of the given number 99

-------------------------------------------------

Please enter any number: 89

89 is a Prime Factor of the given number 89

---------------------------------------------------

Please enter any number: 110

2 is a Prime Factor of the given number 110

 5 is a Prime Factor of the given number 110

 11 is a Prime Factor of the given number 110

------------------------------------------------------

In the above example, we have used the for loop only. Firstly, we have taken the input from the user using the input() method and stored it in a variable num. Then, we have applied the for loop from x=2 to x=num+1. Next, we check if the modulo of the value of x and number is equal to 0. Then we keep the count value = 1 and again apply the for loop inside the for loop from y=2 to y=x//2+1. and check the given if condition. If the condition is satisfied, the count value is set to 0 and we break the statement. Then we come out of the for loop and check the condition if count ==1 and print the value of x. Hence the prime factors are printed with their single-single value.

Program to find prime factors using While loop

Here, we take a number from user input and store it in a variable. Next, we obtain the factors of a number with the help of a nested while loop and find whether the factors are prime or not.

# Python Program to find Prime Factors of a Number
 
num = int(input(" Please enter any number: "))

x = 1

while(x <= num):
    count = 0
    if(num % x == 0):
        y = 1
        while(y <= x):
            if(x % y == 0):
                count = count + 1
            y = y + 1
            
        if (count == 2):
            print(" %d is a Prime Factor of the given number %d" %(x, num))
    x = x + 1

Output of the above code -

Please enter any number: 79

79 is a Prime Factor of the given number 79

---------------------------------------------------

Please enter any number: 90

2 is a Prime Factor of the given number 90

 3 is a Prime Factor of the given number 90

 5 is a Prime Factor of the given number 90

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a program to read two numbers and print thei... >>
<< How do you make a simple calculator in Python?...