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

Published on 12 May 2024 09:40 PM
#Jq

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

Source code in the jq programming language

# Recent versions of jq (version > 1.4) have the following definition of "until":
def until(cond; next):
  def _until:
    if cond then . else (next|_until) end;
  _until;

# relatively_prime(previous) tests whether the input integer is prime
# relative to the primes in the array "previous":
def relatively_prime(previous):
  . as $in
  | (previous|length) as $plen
  # state: [found, ix]
  |  [false, 0]
  | until( .[0] or .[1] >= $plen;
           [ ($in % previous[.[1]]) == 0, .[1] + 1] )
  | .[0] | not ;

# Emit a stream in increasing order of all primes (from 2 onwards)
# that are less than or equal to mx:
def primes(mx):

  # The helper function, next, has arity 0 for tail recursion optimization;
  # it expects its input to be the array of previously found primes:
  def next:
     . as $previous
     | ($previous | .[length-1]) as $last
     | if ($last >= mx) then empty
       else ((2 + $last)
       | until( relatively_prime($previous) ; . + 2)) as $nextp
       | if $nextp <= mx
         then $nextp, (( $previous + [$nextp] ) | next)
	 else empty
         end
       end;
  if mx <= 1 then empty
  elif mx == 2 then 2
  else (2, 3, ( [2,3] | next))
  end
;

# Return an array of the distinct prime factors of . in increasing order
def prime_factors:

  # Return an array of prime factors of . given that "primes"
  # is an array of relevant primes:
  def pf(primes):
    if . <= 1 then []
    else . as $in
    | if ($in | relatively_prime(primes)) then [$in]
      else reduce primes[] as $p
             ([];
              if ($in % $p) != 0 then .
 	      else . + [$p] +  (($in / $p) | pf(primes))
	      end)
      end
      | unique
    end;
    
  if . <= 1 then []
  else . as $in
  | pf( [ primes( (1+$in) | sqrt | floor)  ] )
  end;

# Return an array of prime factors of . repeated according to their multiplicities:
def prime_factors_with_multiplicities:
  # Emit p according to the multiplicity of p
  # in the input integer assuming p > 1
  def multiplicity(p):
    if   .  < p     then empty
    elif . == p     then p
    elif (. % p) == 0 then
       ((./p) | recurse( if (. % p) == 0 then (. / p) else empty end) | p)
    else empty
    end;

  if . <= 1 then []
  else . as $in
  | prime_factors as $primes
  | if ($in|relatively_prime($primes)) then [$in]
    else reduce $primes[]  as $p
           ([];
            if ($in % $p) == 0 then . + [$in|multiplicity($p)] else . end )
    end
  end;

def isalmostprime(k): (prime_factors_with_multiplicities | length) == k;

# Emit a stream of the first N almost-k primes
def almostprimes(N; k):
  if N <= 0 then empty
  else
    # state [remaining, candidate, answer]
    [N, 1, null]
    | recurse( if .[0] <= 0 then empty
	       elif (.[1] | isalmostprime(k)) then [.[0]-1, .[1]+1, .[1]]
	       else [.[0], .[1]+1, null]
               end)
    | .[2] | select(. != null)
  end;

range(1;6) as $k | "k=\($k): \([almostprimes(10;$k)])"

$ jq -c -r -n -f Almost_prime.jq
k=1: [2,3,5,7,11,13,17,19,23,29]
k=2: [4,6,9,10,14,15,21,22,25,26]
k=3: [8,12,18,20,27,28,30,42,44,45]
k=4: [16,24,36,40,54,56,60,81,84,88]
k=5: [32,48,72,80,108,112,120,162,168,176]


  

You may also check:How to resolve the algorithm Array concatenation step by step in the zkl programming language
You may also check:How to resolve the algorithm Sierpinski triangle/Graphical step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Simple turtle graphics step by step in the Julia programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the Ring programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the Vedit macro language programming language