How to resolve the algorithm Tau number step by step in the PL/M programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Tau number step by step in the PL/M programming language

Table of Contents

Problem Statement

A Tau number is a positive integer divisible by the count of its positive divisors.

Show the first   100   Tau numbers. The numbers shall be generated during run-time (i.e. the code may not contain string literals, sets/arrays of integers, or alike).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Tau number step by step in the PL/M programming language

Source code in the pl/m programming language

100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;

/* PRINT NUMBER RIGHT-ALIGNED IN 7 POSITIONS */
PRINT$NUMBER: PROCEDURE (N);
    DECLARE S (7) BYTE INITIAL (' .....$');
    DECLARE N ADDRESS, I BYTE;
    I = 6;
DIGIT:
    I = I - 1;
    S(I) = N MOD 10 + '0';
    N = N / 10;
    IF N > 0 THEN GO TO DIGIT;
    DO WHILE I <> 0;
        I = I - 1;
        S(I) = ' ';
    END;
    CALL PRINT(.S);
END PRINT$NUMBER;

/* COUNT AND STORE AMOUNT OF DIVISORS FOR 1..N AT VEC */
COUNT$DIVS: PROCEDURE (VEC, N);
    DECLARE (VEC, N, V BASED VEC) ADDRESS;
    DECLARE (I, J) ADDRESS;
    
    DO I=1 TO N;
        V(I) = 1;
    END;
    
    DO I=2 TO N;
        J = I;
        DO WHILE J <= N;
            V(J) = V(J) + 1;
            J = J + I;
        END;
    END;
END COUNT$DIVS;

/* GIVEN VECTOR OF COUNT OF DIVISORS, SEE IF N IS A TAU NUMBER */
TAU: PROCEDURE (VEC, N) BYTE;
    DECLARE (VEC, N, V BASED VEC) ADDRESS;
    RETURN N MOD V(N) = 0;
END TAU;

DECLARE AMOUNT LITERALLY '100';
DECLARE LIMIT LITERALLY '1100';

DECLARE SEEN BYTE INITIAL (0);
DECLARE N ADDRESS INITIAL (1);

CALL COUNT$DIVS(.MEMORY, LIMIT);
DO WHILE SEEN < AMOUNT;
    IF TAU(.MEMORY, N) THEN DO;
        CALL PRINT$NUMBER(N);
        SEEN = SEEN + 1;
        IF SEEN MOD 10 = 0 THEN CALL PRINT(.(13,10,'$'));
    END;
    N = N + 1;
END;

CALL EXIT;
EOF

  

You may also check:How to resolve the algorithm Sierpinski triangle step by step in the BCPL programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Subtractive generator step by step in the BASIC programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort step by step in the Ruby programming language
You may also check:How to resolve the algorithm Damm algorithm step by step in the Ada programming language