How to resolve the algorithm Almost prime step by step in the Cowgol programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Almost prime step by step in the Cowgol programming language

Table of Contents

Problem Statement

A   k-Almost-prime   is a natural number

n

{\displaystyle n}

that is the product of

k

{\displaystyle k}

(possibly identical) primes.

1-almost-primes,   where

k

1

{\displaystyle k=1}

,   are the prime numbers themselves. 2-almost-primes,   where

k

2

{\displaystyle k=2}

,   are the   semiprimes.

Write a function/method/subroutine/... that generates k-almost primes and use it to create a table here of the first ten members of k-Almost primes for

1 <= K <= 5

{\displaystyle 1<=K<=5}

.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Almost prime step by step in the Cowgol programming language

Source code in the cowgol programming language

include "cowgol.coh";

sub kprime(n: uint8, k: uint8): (kp: uint8) is
    var p: uint8 := 2;
    var f: uint8 := 0;
    while f < k and p*p <= n loop
        while 0 == n % p loop
            n := n / p;
            f := f + 1;
        end loop;
        p := p + 1;
    end loop;
    if n > 1 then
        f := f + 1;
    end if;
    if f == k then
        kp := 1;
    else
        kp := 0;
    end if;
end sub;

var k: uint8 := 1;
while k <= 5 loop
    print("k = ");
    print_i8(k);
    print(":");
    
    var i: uint8 := 2;
    var c: uint8 := 0;
    while c < 10 loop
        if kprime(i,k) != 0 then
            print(" ");
            print_i8(i);
            c := c + 1;
        end if;
        i := i + 1;
    end loop;
    print_nl();
    k := k + 1;
end loop;

  

You may also check:How to resolve the algorithm Pangram checker step by step in the K programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort with shifting bounds step by step in the Raku programming language
You may also check:How to resolve the algorithm Create an object at a given address step by step in the C programming language