Primality by trial division: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: changed/added comments, add whitespace, simplified the checking for wee primes. -- ~~~~) |
m (→{{header|REXX}}: added checks for non-integers. -- ~~~~) |
||
Line 1,366: | Line 1,366: | ||
/*──────────────────────────────────ISPRIME subroutine──────────────────*/ |
/*──────────────────────────────────ISPRIME subroutine──────────────────*/ |
||
isprime: procedure; parse arg x /*get the number in question*/ |
isprime: procedure; parse arg x /*get the number in question*/ |
||
if \datatype(x,'W') then return 0 /*X isn't an integer? Not prime.*/ |
|||
if wordpos(x,'2 3 5 7')\==0 then return 1 /*is number a teacher's pet?*/ |
if wordpos(x,'2 3 5 7')\==0 then return 1 /*is number a teacher's pet?*/ |
||
if x<2 | x//2==0 | x//3==0 then return 0 /*weed out the riff-raff. */ |
if x<2 | x//2==0 | x//3==0 then return 0 /*weed out the riff-raff. */ |
||
Line 1,423: | Line 1,424: | ||
/*──────────────────────────────────ISPRIME subroutine──────────────────*/ |
/*──────────────────────────────────ISPRIME subroutine──────────────────*/ |
||
isprime: procedure; parse arg x /*get integer to be investigated.*/ |
isprime: procedure; parse arg x /*get integer to be investigated.*/ |
||
if \datatype(x,'W') then return 0 /*X isn't an integer? Not prime.*/ |
|||
/*could also test for non-integer*/ |
|||
/*«IF \DATATYPE(X) THEN RETURN 0»*/ |
|||
if x<11 then return wordpos(x,'2 3 5 7')\==0 /*is it a wee prime? */ |
if x<11 then return wordpos(x,'2 3 5 7')\==0 /*is it a wee prime? */ |
||
if x//2==0 then return 0 /*eliminate the evens. */ |
if x//2==0 then return 0 /*eliminate the evens. */ |