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


 
Module Count_in_factors    {
	Inventory Known1=2@, 3@
	IsPrime=lambda  Known1 (x as decimal) -> {
		=0=1
		if exist(Known1, x) then =1=1 : exit
		if x<=5 OR frac(x) then {if x == 2 OR x == 3 OR x == 5 then Append Known1, x  : =1=1
		Break}
		if frac(x/2) else exit
		if frac(x/3) else exit
		x1=sqrt(x):d = 5@
		{if frac(x/d ) else exit
			d += 2: if d>x1 then Append Known1, x : =1=1 : exit
			if frac(x/d) else exit
			d += 4: if d<= x1 else Append Known1, x :  =1=1: exit
			loop
		}
	}
	decompose=lambda IsPrime (n as decimal) -> {
		Factors=(,)
		{
			k=2@
			While frac(n/k)=0
				n/=k
				Append Factors, (k,)
			End While
			if n=1 then exit
			k++ 
			While frac(n/k)=0
				n/=k
				Append Factors, (k,)
			End While
			if n=1 then exit
			{
				k+=2
				while not isprime(k) {k+=2}
				While frac(n/k)=0
					n/=k : Append Factors, (k,)
				End While
				if n=1 then exit
				loop
			}             
		}
		=Factors
	}
	fold=lambda (a, f$)->{
		Push if$(len(f$)=0->f$, f$+"x")+str$(a,"")
	}
	Print "1=1"
	i=1@
	do
		i++
		Print str$(i,"")+"="+Decompose(i)#fold$(fold,"")
	always
}
Count_in_factors
 