How to resolve the algorithm Primality by trial division step by step in the HicEst programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Primality by trial division step by step in the HicEst 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 HicEst programming language
Source code in the hicest programming language
DO n = 1, 1E6
Euler = n^2 + n + 41
IF( Prime(Euler) == 0 ) WRITE(Messagebox) Euler, ' is NOT prime for n =', n
ENDDO ! e.g. 1681 = 40^2 + 40 + 41 is NOT prime
END
FUNCTION Prime(number)
Prime = number == 2
IF( (number > 2) * MOD(number,2) ) THEN
DO i = 3, number^0.5, 2
IF(MOD(number,i) == 0) THEN
Prime = 0
RETURN
ENDIF
ENDDO
Prime = 1
ENDIF
END
You may also check:How to resolve the algorithm Wasteful, equidigital and frugal numbers step by step in the RPL programming language
You may also check:How to resolve the algorithm Twin primes step by step in the Raku programming language
You may also check:How to resolve the algorithm Non-continuous subsequences step by step in the Standard ML programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Determine if a string has all the same characters step by step in the Pascal programming language