Python Program to Finding out the Prime Factors of a Number

Find the Prime Factors of a Number in Python

Given an integer input as the number, the objective is to Find all the Prime Factors of a the given integer input number. Therefore, we’ll write a program to Find the Prime Factors of a Number in Python Language.

Example
Input : 10
Output : 2 5
Prime Factors of a Number in Python

Find the Prime Factors of a Number in Python Language

Given an integer input as the number, the objective is to find all the prime factors of a Number in Python Language. To do so, we’ll first find all the factors of a Number, meanwhile check whether the number is a Prime or not. If a Number is Even, it’ll has 2 as it’s prime factor. Here are some of the methods we’ll use to solve the above mentioned problem,

  • Method 1: Using Simple Iteration
  • Method 2: Using Recursive Function

We’ll discuss the above mentioned methods in detail in the up coming sections. To know how to check for prime, check out our page, Find Whether or Not the Number is a Prime. Don’t forget to check the blue box below for better understanding of the problem.

Method 1: Using Simple Iteration

Working

In this method we’ll use the concept of loops to find all the factors of a number and to check whether or not the number is a Prime or not. We’ll iterate through numbers in a given range and check if the number is divisible by the numbers or not.

Given an integer input as a number, We’ll perform the following,

  • Run a while loop with condition n>1.
  • Run a nested for loop until (2+n//2).
    • if i == ( 1+ n//2), append the n value and shorten the number by n = n//n.
    • if n%1==0, append the i value and short the n as n= n//i.
  • Return the array and print it.

Let’s implement the above mentioned logic in Python Language.

Python Code

Run
def Prime_Factorial(n):
    if n < 4:
        return n
    arr = []
    while n > 1:
        for i in range(2, int(2+n//2)):
            if i == (1 + n // 2):
                arr.append(n)
                n = n // n
            if n % i == 0:
                arr.append(i)
                n = n // i
                break
    return arr


n = 210
print(Prime_Factorial(n))

Output

[ 2 , 3 , 5 , 7 ]

Method 2: Using Recursive Function

Working

In this method we’ll use recursion to recursively find the factors and check whether they are prime or not. To do so we’ll create a recursive function and recursively check for factors that are prime.

For an integer input, we perform the following,

  • Define a recursive function, Prime_factorial() which takes the number and the output array as arguments.
  • Set the base case as number<4 and step recursive call as Prime_Factorial(number//iter , output_arr).
  • Return the output array and print it.

Let’s implement the above mentioned logic in Python Language. To know more about recursion in Python, check out our page Recursion in Python.

Python Code

Run
def Prime_Factorial(n, arr):
    if n < 4:
        return n
    for i in range(2, int(2+n//2)):
        if i == (1 + n // 2):
            arr.append(n)
            n = 1
            return
        if n % i == 0:
            arr.append(i)
            n = n // i
            Prime_Factorial(n, arr)
            break
    return arr


n = 210
arr = []
print(Prime_Factorial(n, arr))

Output

[ 2, 3, 5, 7 ]

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

8 comments on “Python Program to Finding out the Prime Factors of a Number”


  • SNEHA S

    n=int(input(“enter: “))
    l=[]
    for i in range(2,n):
    while n%i==0:
    l.append(i)
    n=n//i
    print(l)


  • Sharad

    n1 = int(input(“Enter first number:”))

    lst = []
    def find_factors(n1):
    if n1<4:
    lst.append(n1)
    return lst
    for i in range(2,n1):
    while(n1%i==0):
    lst.append(i)
    n1 = n1/i
    return lst

    print(find_factors(n1))


  • Bhoomi

    Num=10
    For I in range(1, num+1) : # find num factors
    If num%i ==0:
    Flag=0
    For j in range(2, i): # for find num is prime or not
    If i%j==0:
    Flag+=1
    Break
    If flag==0:
    Print(i,end=’ ‘)


  • Ganesh

    num = int(input(“rnter the num: “))
    sum = “”
    for i in range(1,10):
    if num % i == 0:
    sum = sum + str(i)
    print(sum)


  • Nilay

    num = int(input(“Enter the number:- “))
    lst= []
    for i in range(2, num+1):
    if num % i == 0:
    flag = 0
    for j in range(2,i):
    if i % j == 0:
    flag = 1
    break
    if flag== 0:
    lst.append(i)
    print(lst)


  • Sourabh

    a=int(input(“Enter any number: “))
    c=[]
    for i in range (1,a+1):
    if a%i==0:
    c.append(i)

    for i in (c):
    if i>1:
    for x in range(2,i):
    if i%x==0:
    break
    else:
    print(i)


  • DS-30

    class prime:
    def prime_1():
    num = 210
    low = 0
    n = []
    b =[]
    for numb in range(low ,num + 1):
    if numb > 1:
    for j in range(2,numb):
    if (numb % j == 0):
    break
    else:
    n.append(numb)
    for i in range(len(n)):
    if ( num % n[i] == 0 ):
    b.append(n[i])
    print(b)
    p1 = prime()
    prime.prime_1()