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


gcd = proc (a, b: int) returns (int)
    while b ~= 0 do
        a, b := b, a//b
    end
    return(a)
end gcd
 
totient = proc (n: int) returns (int)
    tot: int := 0
    for i: int in int$from_to(1,n-1) do
        if gcd(n,i)=1 then tot := tot + 1 end
    end
    return(tot)
end totient
 
perfect = proc (n: int) returns (bool)
    sum: int := 0
    x: int := n
    while true do 
        x := totient(x)
        sum := sum + x
        if x=1 then break end
    end
    return(sum = n)
end perfect
 
start_up = proc ()
    po: stream := stream$primary_output()
    seen: int := 0
    n: int := 3
    while seen < 20 do
        if perfect(n) then
            stream$puts(po, int$unparse(n) || " ")
            seen := seen + 1
        end
        n := n + 2
    end
end start_up