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 CountingInFactors
 
    Sub Main()
        For i As Integer = 1 To 10
            Console.WriteLine("{0} = {1}", i, CountingInFactors(i))
        Next
 
        For i As Integer = 9991 To 10000
            Console.WriteLine("{0} = {1}", i, CountingInFactors(i))
        Next
    End Sub
 
    Private Function CountingInFactors(ByVal n As Integer) As String
        If n = 1 Then Return "1"
 
        Dim sb As New Text.StringBuilder()
 
        CheckFactor(2, n, sb)
        If n = 1 Then Return sb.ToString()
 
        CheckFactor(3, n, sb)
        If n = 1 Then Return sb.ToString()
 
        For i As Integer = 5 To n Step 2
            If i Mod 3 = 0 Then Continue For
 
            CheckFactor(i, n, sb)
            If n = 1 Then Exit For
        Next
 
        Return sb.ToString()
    End Function
 
    Private Sub CheckFactor(ByVal mult As Integer, ByRef n As Integer, ByRef sb As Text.StringBuilder)
        Do While n Mod mult = 0
            If sb.Length > 0 Then sb.Append(" x ")
            sb.Append(mult)
            n = n / mult
        Loop
    End Sub
 
End Module