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

Published on 12 May 2024 09:40 PM

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

Source code in the snobol4 programming language

define('isprime(n)i,max') :(isprime_end)
isprime isprime = n
        le(n,1) :s(freturn)
        eq(n,2) :s(return)
        eq(remdr(n,2),0) :s(freturn)
        max = sqrt(n); i = 1
isp1    i = le(i + 2,max) i + 2 :f(return)
        eq(remdr(n,i),0) :s(freturn)f(isp1)
isprime_end

        define('rprime(n)str,pat1,pat2,m1') :(end_rprime)
rprime  str = dupl('1',n); rprime = n
        pat1 = ('1' | '')
        pat2 = ('11' arbno('1')) $ m1 (*m1 arbno(*m1))
        str pos(0) (pat1 | pat2) rpos(0) :s(freturn)f(return)
end_rprime

*       # Test and display primes 0 .. 50
loop    rprimes = rprimes rprime(n)  ' '
        n = lt(n,50) n + 1 :s(loop)
        output = rprimes 
end

  

You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the RPL programming language
You may also check:How to resolve the algorithm I before E except after C step by step in the Coco programming language
You may also check:How to resolve the algorithm Extreme floating point values step by step in the F# programming language
You may also check:How to resolve the algorithm Dragon curve step by step in the Java programming language
You may also check:How to resolve the algorithm Verify distribution uniformity/Naive step by step in the Quackery programming language