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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Totient function step by step in the Cowgol 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 Cowgol programming language

Source code in the cowgol programming language

include "cowgol.coh";

sub totient(n: uint32): (tot: uint32) is
    tot := n;

    var i: uint32 := 2;
    while i*i <= n loop
        if n%i == 0 then
            while n%i == 0 loop
                n := n/i;
            end loop;
            tot := tot - tot/i;
        end if;
        if i == 2 then
            i := 1;
        end if;
        i := i + 2;
    end loop;

    if n > 1 then
        tot := tot - tot/n;
    end if;
end sub;

var count: uint16 := 0;

print("N\tTotient\tPrime\n");
var n: uint32 := 1;
while n <= 25 loop
    var tot := totient(n);
    print_i32(n);
    print_char('\t');
    print_i32(tot);
    print_char('\t');
    if n-1 == tot then
        count := count + 1;
        print("Yes\n");
    else
        print("No\n");
    end if;
    n := n + 1;
end loop;

print("Number of primes up to 25:\t");
print_i16(count);
print_nl();

while n <= 100000 loop
    tot := totient(n);
    if n-1 == tot then
        count := count + 1;
    end if;
    if n == 100 or n == 1000 or n % 10000 == 0 then
        print("Number of primes up to ");
        print_i32(n);
        print(":\t");
        print_i16(count);
        print_nl();
    end if;
    n := n + 1;
end loop;

  

You may also check:How to resolve the algorithm Zig-zag matrix step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Rep-string step by step in the SETL programming language
You may also check:How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the F# programming language
You may also check:How to resolve the algorithm Linear congruential generator step by step in the K programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the TXR programming language