Solved Problem - Prime Factorization
In this lesson, we'll discuss how to find the prime factorization of a number.
We'll cover the following
Problem statement
Given an integer, , print all the prime factors of .
Input format
A single line of input contains an integer .
Output format
Print all the prime factors of .
Sample
Input 1: `
24
Output 1:
2 3
Input 2:
207
Output 2:
3 23
Explanation
, prime factors are and
, prime factors are and
Solution
Starting from and going up to ; if divides , add this to the list of prime factors and remove all occurrences of by repetitively dividing by .
In the end, if the number is completely factored, we are left with , in which case we are done.
If we do not end up with , that means has a prime factor . This prime factor is the number we are left with since it’s power will be (as discussed earlier).
Get hands-on with 1400+ tech skills courses.