Anti-primes: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: changed title comment. |
→{{header|REXX}}: added a faster 2nd REXX version. |
||
Line 749: | Line 749: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===even and odd numbers=== |
|||
<!-- This REXX version does what the Rosetta Code task requires. Note that the first DO loop is fixed at 59, but the second DO loop if open-ended. Please read/observe the entire REXX program before flagging as incorrect. --- Gerard Schildberger. ~--> |
<!-- This REXX version does what the Rosetta Code task requires. Note that the first DO loop is fixed at 59, but the second DO loop if open-ended. Please read/observe the entire REXX program before flagging as incorrect. --- Gerard Schildberger. ~--> |
||
Line 880: | Line 880: | ||
55 61261200 |
55 61261200 |
||
</pre> |
</pre> |
||
===only even numbers=== |
|||
This REXX version only processes ''even'' numbers (unity is treated as a special case.) |
|||
It's about '''10%''' faster than the 1<sup>st</sup> REXX version. |
|||
<lang rexx>/*REXX program finds and displays N number of anti─primes or highly─composite numbers.*/ |
|||
parse arg N . /*obtain optional argument from the CL.*/ |
|||
if N=='' | N=="," then N=20 /*Not specified? Then use the default.*/ |
|||
@.=.; @.1= 1; @.2= 2; @.4= 3; @.5= 2; @.6= 4 |
|||
say '─index─ ──anti─prime──' /*display a title for the numbers shown*/ |
|||
#= 1 /*the count of anti─primes found " " */ |
|||
maxD= 1 /*the maximum number of divisors so far*/ |
|||
say center(#, 7) right(1, 10) /*display the index and the anti─prime.*/ |
|||
do once=1 for 1 |
|||
do i=2 by 2 to 59 /*step through possible numbers by twos*/ |
|||
d= #divs(i); if d<=maxD then iterate /*get # divisors; Is too small? Skip.*/ |
|||
#= # + 1; maxD= d /*found an anti─prime #; set new minD.*/ |
|||
say center(#, 7) right(i, 10) /*display the index and the anti─prime.*/ |
|||
if #>=N then leave once /*if we have enough anti─primes, done. */ |
|||
end /*i*/ |
|||
do j=60 by 20 /*step through possible numbers by 20. */ |
|||
d= #divs(j); if d<=maxD then iterate /*get # divisors; Is too small? Skip.*/ |
|||
#= # + 1; maxD= d /*found an anti─prime #; set new minD.*/ |
|||
say center(#, 7) right(j, 10) /*display the index and the anti─prime.*/ |
|||
if #>=N then leave once /*if we have enough anti─primes, done. */ |
|||
end /*j*/ |
|||
end /*once*/ |
|||
exit /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
#divs: parse arg x; if @.x\==. then return @.x /*if pre─computed, then return shortcut*/ |
|||
$= 3; y= x%2 |
|||
/* [↑] start with known num of Pdivs.*/ |
|||
do k=3 for x%2-3 while k<y |
|||
if x//k==0 then do /*if no remainder, then found a divisor*/ |
|||
$=$+2; y=x%k /*bump $ Pdivs, calculate limit Y. */ |
|||
if k>=y then do; $= $-1; leave; end /*limit?*/ |
|||
end /* ___ */ |
|||
else if k*k>x then leave /*only divide up to √ x */ |
|||
end /*k*/ /* [↑] this form of DO loop is faster.*/ |
|||
return $+1 /*bump "proper divisors" to "divisors".*/</lang> |
|||
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |