How to resolve the algorithm Primality by trial division step by step in the ERRE programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Primality by trial division step by step in the ERRE 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 ERRE programming language

Source code in the erre programming language

PROGRAM PRIME_TRIAL

PROCEDURE ISPRIME(N%->OK%)
      LOCAL T%
      IF N%<=1 THEN OK%=FALSE  EXIT PROCEDURE END IF
      IF N%<=3 THEN OK%=TRUE EXIT PROCEDURE END IF
      IF (N% AND 1)=0 THEN OK%=FALSE EXIT PROCEDURE END IF
      FOR T%=3 TO SQR(N%) STEP 2 DO
        IF N% MOD T%=0 THEN OK%=FALSE EXIT PROCEDURE END IF
      END FOR
      OK%=TRUE
END PROCEDURE

BEGIN

      FOR I%=1 TO 100 DO
         ISPRIME(I%->OK%)
         IF OK% THEN PRINT(i%;"is prime") END IF
      END FOR

END PROGRAM

  

You may also check:How to resolve the algorithm Horizontal sundial calculations step by step in the D programming language
You may also check:How to resolve the algorithm Euler's sum of powers conjecture step by step in the C programming language
You may also check:How to resolve the algorithm Mian-Chowla sequence step by step in the Phix programming language
You may also check:How to resolve the algorithm Munchausen numbers step by step in the PHP programming language
You may also check:How to resolve the algorithm Runtime evaluation step by step in the BBC BASIC programming language