Problem 77
Prime Factorization
Easy · 10 pts · Math, Number Theory
Problem
You are given an integer N of at least 2. Print its prime factors in non-decreasing order, repeating each prime as many times as it divides N, so that the product of the printed numbers equals N. Trial division works: for d = 2, 3, 4, ..., while d divides N, print d and divide N by d. You only need to try d while d × d <= N; whatever is left above 1 at the end is itself a prime factor. N can be up to 10^12, so its square root is at most 10^6 and this is fast. The numbers in this problem can be larger than a 32-bit int can hold (about 2.1 × 10^9), so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).
Input
A single integer N.
Output
Print the prime factors space-separated.
Constraints
2 <= N <= 1000000000000
Example input
60
Example output
2 2 3 5
Explanation
Dividing 60 by 2 gives 30, dividing by 2 again gives 15, dividing by 3 gives 5, and 5 is prime. So 60 = 2 × 2 × 3 × 5, and the output is 2 2 3 5.