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)

using System.Collections.Generic;
using System.Linq;
using System;

public class hello
{
    public static void Main()
    {
        var a10000 = new List<int>();
        GeneratePrime(a10000, 10000);
        var a = new List<int>();
        var n = int.Parse(Console.ReadLine().Trim());
        Console.Write("{0}:", n);
        if (IsPrime(n) && n > 100000) { Console.Write(" {0}", n); goto end2; }
        var on = n;
        var te = 1;
        foreach (var x in a10000)
        {
            while (true)
            {
                if (n % x == 0)
                {
                    Console.Write(" {0}", x);
                    n /= x;
                    te *= x;
                    if (te == on) goto end;
                }
                else break;
            }
        }
        end:;
        if (te < on)
        {
            n = on / te;
            te = 1;
            on = n;
            GeneratePrime(a, n);
            foreach (var x in a)
            {
                while (true)
                {
                    if (n % x == 0)
                    {
                        Console.Write(" {0}", x);
                        n /= x;
                        te *= x;
                        if (te == on) goto end2;
                    }
                    else break;
                }
            }
        }
        end2:;
        Console.WriteLine();
    }
    public static void GeneratePrime(List<int> primeList, int m)
    {
        int prime;
        var sqrtMax = Math.Sqrt(m);
        var searchList = Enumerable.Range(2, m - 1).ToList();
        do
        {
            prime = searchList.First();
            primeList.Add(prime);
            searchList.RemoveAll(n => n % prime == 0);
        }
        while (prime < sqrtMax);
        primeList.AddRange(searchList);
    }
    public static bool IsPrime(int num)
    {
        if (num < 2) return false;
        else if (num == 2) return true;
        else if (num % 2 == 0) return false; 
        double sqrtNum = Math.Sqrt(num);
        for (int i = 3; i <= sqrtNum; i += 2)
        {
            if (num % i == 0)
            {
                return false;
            }
        }
        return true;
    }
}