How to resolve the algorithm Sequence of primes by trial division step by step in the jq programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sequence of primes by trial division step by step in the jq programming language
Table of Contents
Problem Statement
Generate a sequence of primes by means of trial division.
Trial division is an algorithm where a candidate number is tested for being a prime by trying to divide it by other numbers. You may use primes, or any numbers of your choosing, as long as the result is indeed a sequence of primes. The sequence may be bounded (i.e. up to some limit), unbounded, starting from the start (i.e. 2) or above some given value. Organize your function as you wish, in particular, it might resemble a filtering operation, or a sieving operation. If you want to use a ready-made is_prime function, use one from the Primality by trial division page (i.e., add yours there if it isn't there already).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sequence of primes by trial division step by step in the jq programming language
Source code in the jq programming language
# Produce a (possibly empty) stream of primes in the range [m,n], i.e. m <= p <= n
def primes(m; n):
([m,2] | max) as $m
| if $m > n then empty
elif $m == 2 then 2, primes(3;n)
else (1 + (2 * range($m/2 | floor; (n + 1) /2 | floor))) | select( is_prime )
end;
primes(0;10)
2
3
5
7
[primes(50;99)]
You may also check:How to resolve the algorithm Shortest common supersequence step by step in the Tcl programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the Action! programming language
You may also check:How to resolve the algorithm 24 game/Solve step by step in the Fortran programming language
You may also check:How to resolve the algorithm Factorial step by step in the JOVIAL programming language