Task

Write a program which counts up from   1,   displaying each number as the multiplication of its prime factors.

For the purpose of this task,   1   (unity)   may be shown as itself.



Example

      2   is prime,   so it would be shown as itself.

      6   is not prime;   it would be shown as   



2
×
3


{\displaystyle 2\times 3}

.
2144   is not prime;   it would be shown as   



2
×
2
×
2
×
2
×
2
×
67


{\displaystyle 2\times 2\times 2\times 2\times 2\times 67}

.



Related tasks

   prime decomposition
   factors of an integer
   Sieve of Eratosthenes
   primality by trial division
   factors of a Mersenne number
   trial factoring of a Mersenne number
   partition an integer X into N primes


 
class CountingInFactors {
  function : Main(args : String[]) ~ Nil {
    for(i := 1; i <= 10; i += 1;){
    count := CountInFactors(i);
    ("{$i} = {$count}")->PrintLine();
  };
 
  for(i := 9991; i <= 10000; i += 1;){
    count := CountInFactors(i);
    ("{$i} = {$count}")->PrintLine();
    };
  }
 
  function : CountInFactors(n : Int) ~ String {
    if(n = 1) {
      return "1";
    };
 
    sb := "";
    n := CheckFactor(2, n, sb);
    if(n = 1) {
      return sb;
    };
 
    n := CheckFactor(3, n, sb);
    if(n = 1) {
      return sb;
    };
 
    for(i := 5; i <= n; i += 2;) {
      if(i % 3 <> 0) {
        n := CheckFactor(i, n, sb);
        if(n = 1) {
          break;
        };
      };
    };
 
    return sb;
  }
 
  function : CheckFactor(mult : Int, n : Int, sb : String) ~ Int {
    while(n % mult = 0 ) {
      if(sb->Size() > 0) {
        sb->Append(" x ");
      };
      sb->Append(mult);
      n /= mult;
    };
 
    return n;
  }
}
 