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