Problem:
Prime Factorization

Factorize a given integer n.

Input

n

An integer n is given in a line.

Output

Print the given integer n and :. Then, print prime factors in ascending order. If n is divisible by a prime factor several times, the prime factor should be printed according to the number of times. Print a space before each prime factor.

Constraints

2 ≤ n ≤ 109

Sample Input 1

12

Sample Output 1

12: 2 2 3

Sample Input 2

126

Sample Output 2

126: 2 3 3 7

Solution in C (judge verdict: Accepted)

#include <stdio.h>

#define PRIME_MAX 1000000
int getprimes(int list[], int top){
    int i, j;
    int index = 0;
    char tmp[PRIME_MAX];

    for(i=0;i<top;i++) {
        tmp[i] = 1;
    }
   
    tmp[0] = tmp[1] = 0;
    for(i=2;i<top;i++) {
        if(tmp[i] == 1) {
            /* 素数 */
            list[index] = i;
            index ++;
            for(j=2;j*i<top;j++) {
                tmp[j*i] = 0;
            }
        }
    }

    list[index] = -1;
    return index;
}

int main(void) {
    int i, c;
    int n, base;
    int size;
    int primes[100000];
    int div[1000];
    int d;
    size = getprimes(primes, PRIME_MAX);
   
    while(scanf("%d\n", &base) == 1) {
        i = d = 0;
        n = base;
        while(n > 1) {
        	if((n % primes[i]) == 0) {
        		div[d] = primes[i];
        		n /= primes[i];
        		d++;
        	} else {
        		i++;
        	}
        	
        	if(i >= size) {
        		div[d] = n;
        		d++;
        		break;
        	}
        }
        
        printf("%d:", base);
        for(i=0;i<d;i++) {
        	printf(" %d", div[i]);
        }
        printf("\n");
    }
   
    return 0;
}