How to resolve the algorithm Sequence of primes by trial division step by step in the AWK 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 AWK 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 AWK programming language
Source code in the awk programming language
# syntax: GAWK -f SEQUENCE_OF_PRIMES_BY_TRIAL_DIVISION.AWK
BEGIN {
low = 1
high = 100
for (i=low; i<=high; i++) {
if (is_prime(i) == 1) {
printf("%d ",i)
count++
}
}
printf("\n%d prime numbers found in range %d-%d\n",count,low,high)
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
You may also check:How to resolve the algorithm Spiral matrix step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Associative array/Iteration step by step in the D programming language
You may also check:How to resolve the algorithm Read entire file step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Quine step by step in the AWK programming language
You may also check:How to resolve the algorithm Detect division by zero step by step in the MUMPS programming language