Primality by trial division: Difference between revisions
Content added Content deleted
(Added BBC BASIC) |
m (→{{header|REXX}}: changed/added comments, add whitespace, simplified the checking for wee primes. -- ~~~~) |
||
Line 1,356: | Line 1,356: | ||
p=0 /*a count of primes (so far). */ |
p=0 /*a count of primes (so far). */ |
||
do j=-57 to n /*start in the cellar and work up*/ |
do j=-57 to n /*start in the cellar and work up*/ |
||
if \isprime(j) then iterate |
if \isprime(j) then iterate /*if not prime, keep looking. */ |
||
p=p+1 /*bump the jelly bean counter. */ |
p=p+1 /*bump the jelly bean counter. */ |
||
if |
if j<99 then say right(j,20) 'is prime.' /*Just show wee primes.*/ |
||
say right(j,20) 'is prime.' /*Just blab about the wee primes.*/ |
|||
end /*j*/ |
end /*j*/ |
||
say |
say |
||
Line 1,374: | Line 1,373: | ||
end /*k*/ |
end /*k*/ |
||
return 1 /* |
return 1 /*done dividing, it's prime.*/</lang> |
||
'''output''' when using the default input of 10000 |
'''output''' when using the default input of 10000 |
||
<pre style="height:20ex;overflow:scroll"> |
<pre style="height:20ex;overflow:scroll"> |
||
Line 1,411: | Line 1,410: | ||
<lang rexx>/*REXX program tests for primality using (kinda smartish) trial division*/ |
<lang rexx>/*REXX program tests for primality using (kinda smartish) trial division*/ |
||
parse arg n . /*let user choose how many, maybe*/ |
parse arg n . /*let user choose how many, maybe*/ |
||
if n=='' then n=10000 |
if n=='' then n=10000 /*if not, then assume the default*/ |
||
p=0 /*a count of primes (so far). */ |
p=0 /*a count of primes (so far). */ |
||
do j=-57 to n /*start in the cellar and work up*/ |
do j=-57 to n /*start in the cellar and work up*/ |
||
if \isprime(j) then iterate |
if \isprime(j) then iterate /*if not prime, then keep looking*/ |
||
p=p+1 /*bump the jelly bean counter. */ |
p=p+1 /*bump the jelly bean counter. */ |
||
if |
if j<99 then say right(j,20) 'is prime.' /*Just show wee primes.*/ |
||
say right(j,20) 'is prime.' /*Just blab about the wee primes.*/ |
|||
end /*j*/ |
end /*j*/ |
||
say |
|||
say "There are" p "primes up to" n '(inclusive).' |
say; say "There are" p "primes up to" n '(inclusive).' |
||
exit /*stick a fork in it, we're done.*/ |
exit /*stick a fork in it, we're done.*/ |
||
Line 1,427: | Line 1,425: | ||
/*could also test for non-integer*/ |
/*could also test for non-integer*/ |
||
/*«IF \DATATYPE(X) THEN RETURN 0»*/ |
/*«IF \DATATYPE(X) THEN RETURN 0»*/ |
||
if x<11 |
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//3==0 then return 0 /* ... and eliminate the triples.*/ |
|||
end |
|||
if x//2==0 then return 0 /*eliminate the evens. */ |
|||
if x//3==0 then return 0 /* ... and eliminate the triples.*/ |
|||
/*Note: // is modulus. */ |
/*Note: // is modulus. */ |
||
do k=5 by 6 until k*k>x /*this skips multiples of three. */ |
do k=5 by 6 until k*k>x /*this skips multiples of three. */ |
||
Line 1,440: | Line 1,434: | ||
end /*k*/ |
end /*k*/ |
||
return 1 /* |
return 1 /*did all divisions, it's prime. */</lang> |
||
'''output''' is identical to the first version. |
'''output''' is identical to the first version. |
||
<br><br> |
<br><br> |