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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Almost prime step by step in the Futhark 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 Futhark programming language

Source code in the futhark programming language

let kprime(n: i32, k: i32): bool =
  let (p,f) = (2, 0)
  let (n,_,f) = loop (n, p, f) while f < k && p*p <= n do
    let (n,f) = loop (n, f) while 0 == n % p do
      (n/p, f+1)
    in (n, p+1, f)
  in f + (if n > 1 then 1 else 0) == k

let main(m: i32): [][]i32 =
  let f k =
    let ps = replicate 10 0
    let (_,_,ps) = loop (i,c,ps) = (2,0,ps) while c < 10 do
      if kprime(i,k) then
        unsafe let ps[c] = i
               in (i+1, c+1, ps)
      else (i+1, c, ps)
    in ps
  in map f (1...m)


  

You may also check:How to resolve the algorithm Catalan numbers step by step in the ERRE programming language
You may also check:How to resolve the algorithm Last Friday of each month step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Perfect totient numbers step by step in the bc programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the S-BASIC programming language
You may also check:How to resolve the algorithm Classes step by step in the DWScript programming language