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 Scala (judge verdict: Wrong Answer)

object Main {
  def main(args: Array[String]): Unit = {
    val n = io.StdIn.readInt()
    println(s"$n: ${Primes.primeFactorize(n).mkString(" ")}")
  }
  
  object Primes {
    private def mod_exp(x: BigInt, n: Int, m: Int): BigInt = {
      def loop(x: BigInt, n: Int, acc: BigInt): BigInt = {
        if (n == 0) acc
        else loop (
          (x * x) % m,
          n >> 1,
          if ((n & 1) == 1) (acc * x) % m else acc
        )
      }
      loop(x,n,1)
    }
    private def div_pow_2(d: Int, s: Int = 0): (Int, Int) ={
      if ((d % 2) != 0) (d,s)
      else div_pow_2(d/2, s+1)
    }

    private def miller_rabin(n: Int, a: Int): Boolean = {
      val (d,s): (Int, Int) = div_pow_2(n-1)
      val a_exp_d: BigInt = mod_exp(a,d,n)
      def double(x: BigInt): BigInt = mod_exp(x,2,n)
      def loop(e: BigInt, i: Int): Boolean = {
        if (i == 0) false
        else if (e == n-1) true
        else loop(double(e), i-1)
      }
      a_exp_d == 1 || loop(a_exp_d, s)
    }
    def miller_rabin_test(n: Int, k: Int = 20): Boolean = {
      def loop(i: Int): Boolean = {
        if (i == 0) true
        else {
          val a = scala.util.Random.nextInt(n-1)+1
          if (miller_rabin(n,a)) loop(i-1)
          else false
        }
      }
      if (n < 2) false
      else loop(k)
    }
    def primeFactorize(n: Int): Seq[Int] = {
      val ab = collection.mutable.ArrayBuffer.empty[Int]
      var x = n
      while (x !=1) {
        if (isPrime(x)) {ab += x; x = 1}
        else {
          val p = rho(x)
          if (isPrime(p))
            while (x % p == 0) {
              x /= p
              ab += p
            }
          else {
            val ps = primeFactorize(p)
            ps foreach (p => while (x % p == 0) {x /= p; ab += p})
          }
        }
      }
      ab
    }
    def rho(n: Int): Int = {
      def f(x: Int, c: Int): Int = (x * x + c) % n
      def loop(x: Int, y: Int, d: Int, c: Int = 1): Int =
        if (d != 1) {
          if (d == n) loop(2,2,1,c+1)
          else d
        }
        else {
          val a = f(x, c)
          val b = f(f(y, c), c)
          loop(a, b, gcd(Math.abs(a-b), n), c)
        }
      if (n % 2 == 0) 2
      else loop(2,2,1)
    }
    def gcd(x: Int, y: Int): Int = {
      if (x < y) gcd(y, x)
      else {
        if (y > 0) {
          val r = x % y
          gcd(y, r)
        } else x
      }
    }
    def isPrime(n: Int): Boolean = Primes.miller_rabin_test(n)
  }
}