GoCoding.org

Program to find all the prime factors of the given number

by | Dec 26, 2020 | Python Programs

Introduction

The task is to print all the prime numbers of the given positive integer.

Program to find all the prime factors of the given number

Program

import math 
def find_prime(ip_no): 
    ip_no = int(ip_no)
# Find total number of 2's dividing the num
    while ip_no % 2 == 0: 
        print("2") 
        ip_no = ip_no / 2
    for num in range(3,int(math.sqrt(ip_no))+1,2): 
        while ip_no % num== 0: 
            print(num)
            ip_no = ip_no / num 
    if ip_no > 2: 
        print(ip_no)
        
ip_no = input("Enter the positive integer: ")
find_prime(ip_no)

 

Output

all the prime factors Output

Explanation

In the above python program, the steps followed are:

  • Find whether the number is divisible by 2, if yes then print 2 and return the number after dividing.
  • Now, divide the resulting number by numbers (num) in range 3 to square root number. If num divides the integer, print the value of num and increment it by 2.
  • If num is greater than 2, print the num.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.