How to resolve the algorithm Primality by trial division step by step in the Forth programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Primality by trial division step by step in the Forth programming language
Table of Contents
Problem Statement
Write a boolean function that tells whether a given integer is prime.
Remember that 1 and all non-positive numbers are not prime. Use trial division. Even numbers greater than 2 may be eliminated right away. A loop from 3 to √ n will suffice, but other loops are allowed.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Primality by trial division step by step in the Forth programming language
Source code in the forth programming language
: prime? ( n -- f )
dup 2 < if drop false
else dup 2 = if drop true
else dup 1 and 0= if drop false
else 3
begin 2dup dup * >=
while 2dup mod 0=
if 2drop false exit
then 2 +
repeat 2drop true
then then then ;
You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the Forth programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Lua programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the APL programming language
You may also check:How to resolve the algorithm Variable size/Set step by step in the Phix programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the COBOL programming language