Given a number n, write an efficient function to print all prime factors of n.
For example, if the input number is 12, the output should be “2 2 3”.
And if the input number is 315, the output should be “3 3 5 7”.
1) While n is divisible by 2, print 2 and divide n by 2.
2) After step 1, n must be odd. Now start a loop from i = 3 to the square root of n. While i divides n, print i and divide n by i, increment i by 2 and continue.
3) If n is a prime number and is greater than 2, then n will not become 1 by above two steps. So print n if it is greater than 2.
reference
Code:
Problem:
UVA 583 - Prime Factors
0 Comments
If you have any doubts, Please let me know