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

Published on 12 May 2024 09:40 PM

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

Source code in the pascal programming language

program CountInFactors(output);

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}

type
  TdynArray = array of integer;

function factorize(number: integer): TdynArray;
var
  k: integer;
begin
  if number = 1 then
  begin
    setlength(Result, 1);
    Result[0] := 1
  end
  else
  begin
    k := 2;
    while number > 1 do
    begin
      while number mod k = 0 do
      begin
        setlength(Result, length(Result) + 1);
        Result[high(Result)] := k;
        number := number div k;
      end;
      inc(k);
    end;
  end
end;

var
  i, j: integer;
  fac: TdynArray;

begin
  for i := 1 to 22 do
  begin
    write(i, ':  ' );
    fac := factorize(i);
    write(fac[0]);
    for j := 1 to high(fac) do
      write(' * ', fac[j]);
    writeln;
  end;
end.


  

You may also check:How to resolve the algorithm Reduced row echelon form step by step in the Tcl programming language
You may also check:How to resolve the algorithm Convert decimal number to rational step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Delegates step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Ring programming language
You may also check:How to resolve the algorithm Arrays step by step in the Lua programming language