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



Related task

   Totient function


Also see

   the OEIS entry for   perfect totient numbers.
   mrob   list of the first 54


include "cowgol.coh";
 
sub gcd(a: uint16, b: uint16): (r: uint16) is
    while b != 0 loop
        r := a;
        a := b;
        b := r % b;
    end loop;
    r := a;
end sub;
 
sub totient(n: uint16): (tot: uint16) is
    var i: uint16 := 1;
    tot := 0;
    while i < n loop
        if gcd(n,i) == 1 then
            tot := tot + 1;
        end if;
        i := i + 1;
    end loop;
end sub;
 
sub totientSum(n: uint16): (sum: uint16) is
    var x := n;
    sum := 0;
    while x > 1 loop
        x := totient(x);
        sum := sum + x;
    end loop;
end sub;
 
var seen: uint8 := 0;
var n: uint16 := 3;
while seen < 20 loop
    if totientSum(n) == n then
        print_i16(n);
        print_char(' ');
        seen := seen + 1;
    end if;
    n := n + 2;
end loop;
print_nl();