How do you find the prime factor of a number in python while loop?

In this post, 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.

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 -

How do you find the prime factor of a number in python while loop?

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 -

How do you find the prime factor of a number in python while loop?

Using while loop and for loop

In the given python program, we have used the while loop and for loop both for finding out the prime factors of the given number.

# Python Program to find Prime Factors of a Number
 
import math

def primefactors(num):
   #even number divisible
   while num % 2 == 0:
      print(2),
      num = num / 2
    
   #n became odd
   for x in range(3,int(math.sqrt(num))+1,2):
     
      while (num % x == 0):
         print(x)
         num = num / x

   # if num is prime 
   if num > 2:
      print(num)
 
num = int(input("Please enter any number : "))
primefactors(num)

Output of the above code -

How do you find the prime factor of a number in python while loop?

  • Introduction
  • What is a prime factor of a number?
  • Steps to find the prime factors of a number
  • Examples of Printing the Prime Factors of a Number in Python
    • 1. Prime Factor of a number in Python using While and for loop
    • 2. using for loop only
    • 3. Prime Factor Of A Number In Python using while loop only
  • Conclusion

Introduction

In this article, we will see a python program to print all the prime factors of the given number. If a number is a prime number and perfectly divides the given number then that number is said to be a prime factor of the given number. Here, we will see what is a prime factor, a method to find a prime factor, and the python program.

What is a prime factor of a number?

Prime factors of a number are the prime number which on multiplying together gives the number. we can check the prime factor of a number by two conditions:

  • The number should be a prime number.
  • The number should divide the number perfectly.

How do you find the prime factor of a number in python while loop?

  1. Let the number be denoted by num.
  2. while num is divisible by 2, we will print 2 and divide the num by 2.
  3. After step 2, num must be always odd.
  4. Start a loop from I = 3 to the square root of n. If i divide num, print i, and divide num by i. After i fail to divide num, increment the i value by 2 and continue.
  5. If num is a prime number and is greater than 2, then the num cannot become 1.
  6. So, print num if it is greater than 2.

Examples of Printing the Prime Factors of a Number in Python

Let us understand the program for prime factors of the number in details with the help of different examples:

1. Prime Factor of a number in Python using While and for loop

In this program, We will be using while loop and for loop both for finding out the prime factors of the given number. we will import the math module in this program so that we can use the square root function in python. After that, we will apply the loop and try to find the prime factors of the given number.

# python program to print prime factors

import math
def primefactors(n):
   #even number divisible
   while n % 2 == 0:
      print (2),
      n = n / 2
   
   #n became odd
   for i in range(3,int(math.sqrt(n))+1,2):
    
      while (n % i == 0):
         print (i)
         n = n / i
   
   if n > 2:
      print (n)

n = int(input("Enter the number for calculating the prime factors :\n"))
primefactors(n)

Output:

Enter the number for calculating the prime factors :
650
2
5
5
13

Explanation:

Here firstly we have imported the math library from the python module. Secondly, we have taken the input n as the number and called the function primefactors(). Thirdly, we have taken primefactors() as the function and applied while loop and checked if the modulo of the number is coming 0 by dividing it by 2. Fourthly, the while loop will be executed till the number is even and divisible by 2 and printing it and divide the number by 2 at each iteration. After that, we will apply the for loop from ‘i=3’ till the square root of n+1. Then again we will apply while loop inside the for loop and check the condition. At last, if n is greater than 2 then we have printed the number. Hence, all the prime factors of the number get printed.

2. using for loop only

In this program, We will be using for loop only for finding out the prime factors of the given number. we will be applying multiple for loops and try to find the prime factors of the given number.

#using for loop

n = int(input("Enter the number for calculating the prime factors :\n"))
for i in range(2,n + 1):
    if n % i == 0:
        count = 1
        for j in range(2,(i//2 + 1)):
            if(i % j == 0):
                count = 0
                break
        if(count == 1):
            print(i)

Output:

Enter the number for calculating the prime factors :
350
2
5
7

Explanation:

In this example, we will be using for loop only. Firstly, we have taken the input from the user as n. Secondly, we have applied the for loop from i=2 to i=n+1. Then, we will check if the modulo of the value of i and number is equal to 0. Then we keep the count value = 1 and again apply the for loop inside the for loop from j=2 to j=i//2+1. and check the given if condition. if the condition satisfies count value is set to 0 and we break the statement. We come out of the for loop and check the condition if count ==1 and print the value of i. hence the primefactors are printed with the single-single value of them.

NOTE:

Suppose we take input as 650 : the prime factors are 2,5,5,13
so, it will print 2,5,13 as all integers one time.

3. Prime Factor Of A Number In Python using while loop only

In this program, We will be using a while loop only for finding out the prime factors of the given number. we will be applying multiple while loops and try to find the prime factors of the given number.

#using while loop

n = int(input("Enter any Number for calculating the prime factors: "))
i = 1

while(i <= n):
    c = 0
    if(n % i == 0):
        j = 1
        while(j <= i):
            if(i % j == 0):
                c = c + 1
            j = j + 1
            
        if (c == 2):
            print(i)
    i = i + 1

Output:

Enter any Number for calculating the prime factors: 350
2
5
7

Explanation:

In this example, we will be using a while loop only. Firstly, we have taken the input from the user as n. Secondly, we will set i value as 1. Thirdly, we will apply a while loop with checking condition as i should be less than or equal to n. Inside the loop, we will set the c value as 0 and apply the if and while the condition in it. At last, we will be checking if the value of c becomes equal to 2 then we print the value of i. hence, the prime factors are printed with the single-single value of them.

NOTE:

Suppose we take input as 650 : the prime factors are 2,5,5,13
so, it will print 2,5,13 as all integers one time.

Conclusion

In this tutorial, we have seen how to find the prime factors of the number with 3 different methods. All the methods are explained in detail with the help of examples and their explanation. You can use any method which you like according to your need.

How do you find the prime number in a while loop in Python?

Show activity on this post. flag = 0 n = int(input('\nEnter whole number to check : ')) i = 2 while i <= (n/2): if (n%i) == 0: flag = 1 break if n == 1: print('1 is neither prime nor composite') elif flag == 0: print(n,' is a prime number.

How do you find the factors of a number in a while loop in Python?

Find Factors of a Number using for Loop print("Enter a Number: ", end="") try: num = int(input()) print("\nFactors of " +str(num)+ " are: ", end="") for i in range(1, num+1): if num % i == 0: print(i, end=" ") print() except ValueError: print("\nInvalid Input!

How do you find prime numbers in Python?

Example 1: Using a flag variable We check if num is exactly divisible by any number from 2 to num - 1 . If we find a factor in that range, the number is not prime, so we set flag to True and break out of the loop. Outside the loop, we check if flag is True or False . If it is True , num is not a prime number.

How do you know if a number is prime in a for loop?

Program to Check Prime Number In the program, a for loop is iterated from i = 2 to i < n/2 . If n is perfectly divisible by i , n is not a prime number. In this case, flag is set to 1, and the loop is terminated using the break statement.