How to resolve the algorithm Perfect totient numbers step by step in the ALGOL 68 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Perfect totient numbers step by step in the ALGOL 68 programming language

Table of Contents

Problem Statement

Generate and show here, the first twenty Perfect totient numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Perfect totient numbers step by step in the ALGOL 68 programming language

Source code in the algol programming language

BEGIN # find the first 20 perfect totient numbers #
    # returns the number of integers k where 1 <= k <= n that are mutually prime to n #
    PROC totient = ( INT n )INT: # algorithm from the second Go sample #
        IF   n < 3 THEN 1
        ELIF n = 3 THEN 2
        ELSE
            INT result := n;
            INT v      := n;
            INT i      := 2;
            WHILE i * i <= v DO
                IF v MOD i = 0 THEN
                    WHILE v MOD i = 0 DO v OVERAB i OD;
                    result -:= result OVER i
                FI;
                IF i = 2 THEN
                   i := 1
                FI;
                i +:= 2
            OD;
            IF v > 1 THEN result -:= result OVER v FI;
            result
         FI # totient # ;
    # find the first 20 perfect totient numbers #
    INT p count := 0;
    FOR i FROM 2 WHILE p count < 20 DO
        INT t   := totient( i );
        INT sum := t;
        WHILE t /= 1 DO
            t    := totient( t );
            sum +:= t
        OD;
        IF sum = i THEN
            # have a perfect totient #
            p count +:= 1;
            print( ( " ", whole( i, 0 ) ) )
        FI
    OD;
    print( ( newline ) )        
END

  

You may also check:How to resolve the algorithm Bitmap/Histogram step by step in the OCaml programming language
You may also check:How to resolve the algorithm Sylvester's sequence step by step in the Prolog programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Perl programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the J programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the TXR programming language