How to resolve the algorithm Totient function step by step in the SETL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Totient function step by step in the SETL programming language

Table of Contents

Problem Statement

The   totient   function is also known as:

The totient function:

If the totient number   (for N)   is one less than   N,   then   N   is prime.

Create a   totient   function and: Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Totient function step by step in the SETL programming language

Source code in the setl programming language

program totient;
    loop for n in [1..1000000] do
        tot := totient(n);
        if tot = n-1 then prime +:= 1; end if;

        if n <= 25 then
            print(lpad(str n, 2), "  ",
                  lpad(str tot, 2), "  ",
                  if tot = n-1 then "prime" else "" end if);
        end if;

        if n in [1000,10000,100000,1000000] then
            print(lpad(str prime,8), "primes up to" + lpad(str n,8));
        end if;
    end loop;

    proc totient(n);
        tot := n;
        i := 2;
        loop while i*i <= n do
            if n mod i = 0 then
                loop while n mod i = 0 do
                    n div:= i;
                end loop;
                tot -:= tot div i;
            end if;
            if i=2 then i:=3;
            else i+:=2;
            end if;
        end loop;
        if n>1 then
            tot -:= tot div n;
        end if;
        return tot;
    end proc;
end program;

  

You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Fibonacci word step by step in the Ada programming language
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the Python programming language
You may also check:How to resolve the algorithm Runge-Kutta method step by step in the PL/I programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the PowerShell programming language