How to resolve the algorithm Product of min and max prime factors step by step in the Cowgol programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Product of min and max prime factors step by step in the Cowgol programming language

Table of Contents

Problem Statement

Exactly as the task title implies.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Product of min and max prime factors step by step in the Cowgol programming language

Source code in the cowgol programming language

include "cowgol.coh";

const MAX := 100;
var prime: uint8[MAX+1];
typedef N is @indexof prime;

sub Sieve() is
    prime[0] := 0;
    prime[1] := 0;
    MemSet(&prime[2], 1, @bytesof prime-2);    

    var p: N := 2;
    while p*p <= MAX loop
        var c: N := p*p;
        while c <= MAX loop
            prime[c] := 0;  
            c := c + p;
        end loop;
        p := p + 1;
    end loop;
end sub;

sub LowFactor(n: N): (f: N) is
    if n == 1 then f := 1; return; end if;
    f := 2;
    while f <= n loop
        if prime[f] == 1 and n%f == 0 then return; end if;
        f := f + 1;
    end loop;
end sub;

sub HighFactor(n: N): (f: N) is
    if n == 1 then f := 1; return; end if;
    f := n;
    while f >= 2 loop
        if prime[f] == 1 and n%f == 0 then return; end if;
        f := f - 1;
    end loop;
end sub;

Sieve();
var i: N := 1;
while i <= MAX loop
    print_i16(LowFactor(i) as uint16 * HighFactor(i) as uint16);
    if i % 10 == 0
        then print_nl();
        else print_char('\t');
    end if;
    i := i + 1;
end loop;

  

You may also check:How to resolve the algorithm Introspection step by step in the AWK programming language
You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the Lua programming language
You may also check:How to resolve the algorithm Find largest left truncatable prime in a given base step by step in the F# programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the VBA programming language