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


 
      Inventory Known1=2@, 3@
      IsPrime=lambda  Known1 (x as decimal) -> {
            =0=1
            if exist(Known1, x) then =1=1 : exit
            if x<=5 OR frac(x) then {if x == 2 OR x == 3 OR x == 5 then Append Known1, x  : =1=1
            Break}
            if frac(x/2) else exit
            if frac(x/3) else exit
            x1=sqrt(x):d = 5@
            {if frac(x/d ) else exit
                  d += 2: if d>x1 then Append Known1, x : =1=1 : exit
                  if frac(x/d) else exit
                  d += 4: if d<= x1 else Append Known1, x :  =1=1: exit
             loop}
      }
 
      i=2
      While Len(Known1)<20 {
            dummy=Isprime(i)            
            i++
      }
      Print "first ";len(known1);" primes"
      Print Known1
      Print "From 110 to 130"
      count=0
      For i=110 to 130 {
            If isPrime(i) Then Print i, : count++
      }
      Print
      Print "Found ";count;" primes"
 