Solved Problem - Prime Factorization

In this lesson, we'll discuss how to find the prime factorization of a number.

Problem statement

Given an integer, NN, print all the prime factors of NN.

Input format

A single line of input contains an integer NN (1N1010)(1 \leq N \leq 10^{10}).

Output format

Print all the prime factors of NN.


Sample

Input 1: `

24

Output 1:

2 3

Input 2:

207

Output 2:

3 23

Explanation

24=23×324 = 2^{3} \times 3, prime factors are 22 and 33

207=32×23207 = 3^{2}\times23, prime factors are 33 and 2323


Solution

Starting from i=2i=2 and going up to N\sqrt{N}; if ii divides NN, add this to the list of prime factors and remove all occurrences of ii by repetitively dividing NN by ii.

In the end, if the number is completely factored, we are left with 11, in which case we are done.

If we do not end up with 11, that means NN has a prime factor >N> \sqrt{N}. This prime factor is the number we are left with since it’s power will be 11 (as discussed earlier).

Get hands-on with 1200+ tech skills courses.