How to resolve the algorithm Count in factors step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Count in factors step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

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.

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}

.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Count in factors step by step in the FreeBASIC programming language

Source code in the freebasic programming language

' FB 1.05.0 Win64

Sub getPrimeFactors(factors() As UInteger, n As UInteger)
  If n < 2 Then Return
  Dim factor As UInteger = 2
  Do
    If n Mod factor = 0 Then
      Redim Preserve factors(0 To UBound(factors) + 1)
      factors(UBound(factors)) = factor
      n \= factor
      If n = 1 Then Return
    Else
      factor += 1  
    End If    
  Loop
End Sub 

Dim factors() As UInteger

For i As UInteger = 1 To 20
  Print Using "##"; i;
  Print " = ";   
  If i > 1 Then 
    Erase factors
    getPrimeFactors factors(), i
    For j As Integer = LBound(factors) To UBound(factors)
      Print factors(j);
      If j < UBound(factors) Then Print " x ";
    Next j
    Print
  Else
    Print i
  End If
Next i 

Print
Print "Press any key to quit"
Sleep

  

You may also check:How to resolve the algorithm Own digits power sum step by step in the jq programming language
You may also check:How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the Python programming language
You may also check:How to resolve the algorithm Long multiplication step by step in the Batch File programming language
You may also check:How to resolve the algorithm Shoelace formula for polygonal area step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Ray-casting algorithm step by step in the JavaScript programming language