Anonymous user
Primality by trial division: Difference between revisions
m
→{{header|REXX}}: changed some comments. -- ~~~~
m (→{{header|REXX}}: added checks for non-integers. -- ~~~~) |
m (→{{header|REXX}}: changed some comments. -- ~~~~) |
||
Line 1,366:
/*──────────────────────────────────ISPRIME subroutine──────────────────*/
isprime: procedure; parse arg x /*get the number in question*/
if \datatype(x,'W') then return 0 /*X
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. */
/*Note: // is modulus. */▼
do k=5 by 6 until k*k > x
if x//k==0 | x//(k+2)==0 then return 0 /*do a pair of divides (mod)*/
end /*k*/
▲ /*Note: // is modulus. */
return 1 /*done dividing, it's prime.*/</lang>
'''output''' when using the default input of 10000
Line 1,408:
===unrolled version===
This version uses an ''unrolled'' version (of the first version) of the multiple-clause '''IF''' statements, and
<br>also an optimized version of the testing of low primes, making it about
<lang rexx>/*REXX program tests for primality using (kinda smartish) trial division*/
parse arg n . /*let user choose how many, maybe*/
|