How to resolve the algorithm Sequence of primes by trial division step by step in the REXX 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 REXX 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 REXX programming language
Source code in the rexx programming language
/*REXX program lists a sequence of primes by testing primality by trial division. */
parse arg n . /*get optional number of primes to find*/
if n=='' | n=="," then n= 26 /*Not specified? Then use the default.*/
tell= (n>0); n= abs(n) /*Is N negative? Then don't display.*/
@.1=2; if tell then say right(@.1, 9) /*display 2 as a special prime case. */
#=1 /*# is number of primes found (so far)*/
/* [↑] N: default lists up to 101 #s.*/
do j=3 by 2 while #
/* [↓] divide by the primes. ___ */
do k=2 to # while !.k<=j /*divide J with all primes ≤ √ J */
if j//@.k==0 then iterate j /*÷ by prev. prime? ¬prime ___ */
end /*j*/ /* [↑] only divide up to √ J */
#= #+1 /*bump the count of number of primes. */
@.#= j; !.#= j*j /*define this prime; define its square.*/
if tell then say right(j, 9) /*maybe display this prime ──► terminal*/
end /*j*/ /* [↑] only display N number of primes*/
/* [↓] display number of primes found.*/
say # ' primes found.' /*stick a fork in it, we're all done. */
/*REXX program lists a sequence of primes by testing primality by trial division. */
parse arg N . /*get optional number of primes to find*/
if N=='' | N=="," then N= 26 /*Not specified? Then assume default.*/
tell= (N>0); N= abs(N) /*N is negative? Then don't display. */
@.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; #= 5; s= @.# + 2
/* [↑] is the number of low primes.*/
do p=1 for # while p<=N /* [↓] find primes, and maybe show 'em*/
if tell then say right(@.p, 9) /*display some pre─defined low primes. */
!.p= @.p**2 /*also compute the squared value of P. */
end /*p*/ /* [↑] allows faster loop (below). */
/* [↓] N: default lists up to 101 #s.*/
do j=s by 2 while #
parse var j '' -1 _ /*obtain the last digit of the J var.*/
if _ ==5 then iterate /*is this integer a multiple of five? */
if j // 3 ==0 then iterate /* " " " " " " three? */
if j // 7 ==0 then iterate /* " " " " " " seven? */
if j //11 ==0 then iterate /* " " " " " " eleven?*/
/* [↓] divide by the primes. ___ */
do k=p to # while !.k<=j /*divide J by other primes ≤ √ J */
if j//@.k ==0 then iterate j /*÷ by prev. prime? ¬prime ___ */
end /*k*/ /* [↑] only divide up to √ J */
#= #+1 /*bump the count of number of primes. */
@.#= j; !.#= j*j /*define this prime; define its square.*/
if tell then say right(j, 9) /*maybe display this prime ──► terminal*/
end /*j*/ /* [↑] only display N number of primes*/
/* [↓] display number of primes found.*/
say # ' primes found.' /*stick a fork in it, we're all done. */
You may also check:How to resolve the algorithm Collections step by step in the Nim programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Tcl programming language
You may also check:How to resolve the algorithm Search a list of records step by step in the Arturo programming language
You may also check:How to resolve the algorithm Fast Fourier transform step by step in the J programming language
You may also check:How to resolve the algorithm Permutations/Rank of a permutation step by step in the Kotlin programming language