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:
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 /*if not prime, keep looking. */
p=p+1 /*bump the jelly bean counter. */
if length(j)>2<99 then iteratesay right(j,20) 'is prime.' /*onlyJust show two-digitwee primes. */
say right(j,20) 'is prime.' /*Just blab about the wee primes.*/
end /*j*/
say
Line 1,374 ⟶ 1,373:
end /*k*/
 
return 1 /*I'mdone exhausteddividing, it's prime!.*/</lang>
'''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 /*if not, then assume the default*/
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 /*if not prime, then keep looking*/
p=p+1 /*bump the jelly bean counter. */
if length(j)>2<99 then iteratesay right(j,20) 'is prime.' /*onlyJust show two-digit wee primes.*/
say right(j,20) 'is prime.' /*Just blab about the wee primes.*/
end /*j*/
 
say
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 then do then return wordpos(x,'2 3 5 7')\==0 /*testis for (low)it speciala cases.wee prime? */
if x//2==0 then return 0 if wordpos(x,'2 3 5 7')\==0 then return/*eliminate the evens. 1 /*is wee prime?*/
if x//3==0 then return 0 /* ... and /*weed outeliminate the other riff-rafftriples. */
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. */
do k=5 by 6 until k*k>x /*this skips multiples of three. */
Line 1,440 ⟶ 1,434:
end /*k*/
 
return 1 /*Whew,did I'mall exhausteddivisions, it's prime. */</lang>
'''output''' is identical to the first version.
<br><br>