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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Count in factors step by step in the PureBasic 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 PureBasic programming language

Source code in the purebasic programming language

Procedure Factorize(Number, List Factors())
  Protected I = 3, Max
  ClearList(Factors())
  While Number % 2 = 0
    AddElement(Factors())
    Factors() = 2
    Number / 2
  Wend
  Max = Number
  While I <= Max And Number > 1
    While Number % I = 0
      AddElement(Factors())
      Factors() = I
      Number / I
    Wend
    I + 2
  Wend
EndProcedure

If OpenConsole()
  NewList n()
  For a=1 To 20
    text$=RSet(Str(a),2)+"= "
    Factorize(a,n())
    If ListSize(n())
      ResetList(n())
      While NextElement(n())
        text$ + Str(n())
        If ListSize(n())-ListIndex(n())>1
          text$ + "*"
        EndIf
      Wend
    Else
      text$+Str(a) ; To handle the '1', which is not really a prime...
    EndIf
    PrintN(text$)
  Next a
EndIf

  

You may also check:How to resolve the algorithm Set step by step in the Erlang programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Singleton step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Partial function application step by step in the E programming language
You may also check:How to resolve the algorithm Faces from a mesh step by step in the C++ programming language