Task

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.



Related tasks

   count in factors
   prime decomposition
   AKS test for primes
   factors of an integer
   Sieve of Eratosthenes
   factors of a Mersenne number
   trial factoring of a Mersenne number
   partition an integer X into N primes
   sequence of primes by Trial Division


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