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

Published on 12 May 2024 09:40 PM

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

Source code in the algol programming language

begin % show numbers and their prime factors                                 %
    % shows nand its prime factors                                           %
    procedure showFactors ( integer value n ) ;
        if n <= 3 then write( i_w := 1, s_w := 0, n, ": ", n )
        else begin
            integer v, f; logical first;
            first := true;
            v     := n;
            write( i_w := 1, s_w := 0, n, ": " );
            while not odd( v ) and v > 1 do begin
                if not first then writeon( s_w := 0, " x " );
                writeon( i_w := 1, s_w := 0, 2 );
                v     := v div 2;
                first := false
            end while_not_odd_v ;
            f := 1;
            while v > 1 do begin
                f := f + 2;
                while v rem f = 0 do begin
                    if not first then writeon( s_w := 0, " x " );
                    writeon( i_w := 1, s_w := 0, f );
                    v         := v div f;
                    first := false
                end while_v_rem_f_eq_0
            end while_v_gt_0_and_f_le_v
        end showFactors ;

    % show the factors of various ranges - same as Wren                      %
    for i :=    1 until    9 do showFactors( i );
    write( "... " );
    for i := 2144 until 2154 do showFactors( i );
    write( "... " );
    for i := 9987 until 9999 do showFactors( i )
end.

  

You may also check:How to resolve the algorithm Jump anywhere step by step in the Logtalk programming language
You may also check:How to resolve the algorithm Stack step by step in the Java programming language
You may also check:How to resolve the algorithm Animation step by step in the Java programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the min programming language
You may also check:How to resolve the algorithm Terminal control/Dimensions step by step in the Retro programming language