How to resolve the algorithm Primality by trial division step by step in the Draco programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Primality by trial division step by step in the Draco 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 Draco programming language
Source code in the draco programming language
proc prime(word n) bool:
word factor;
bool composite;
if n<=4 then
n=2 or n=3
elif n&1 = 0 then
false
else
factor := 3;
composite := false;
while not composite and factor*factor <= n do
composite := n % factor = 0;
factor := factor + 2
od;
not composite
fi
corp
proc main() void:
word i;
for i from 0 upto 100 do
if prime(i) then
writeln(i)
fi
od
corp
You may also check:How to resolve the algorithm Empty program step by step in the Odin programming language
You may also check:How to resolve the algorithm Execute HQ9+ step by step in the Go programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the ALGOL-M programming language
You may also check:How to resolve the algorithm Bell numbers step by step in the Java programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the DCL programming language