Table of Contents
Introduction
The task is to find largest prime factor of a given positive integer.
Program
import math def find(ip_int): large_prime = -1 ip_int = int(ip_int) # Find total number of 2’s dividing the number while ip_int % 2 == 0: large_prime = 2 ip_int /= 2 # Find all prime numbers for num in range(3, int(math.sqrt(ip_int)) + 1, 2): while ip_int % num == 0: large_prime= num ip_int = ip_int / num if ip_int > 2: large_prime = ip_int return int(large_prime) ip_int = input("Enter the positive integer: ") print("The largest prime factor is: ", find(ip_int))
Output:
Explanation
In the code, we have found all the prime factors of the number 42 i.e. 2 * 3 * 7 and returned the largest number.
0 Comments