Anti-primes: Difference between revisions

1,183 bytes removed ,  2 months ago
→‎{{header|REXX}}: ooRexx compatible and dramativally simplified
m (→‎{{header|Wren}}: Minor tidy)
(→‎{{header|REXX}}: ooRexx compatible and dramativally simplified)
Line 3,846:
 
The   #DIVS   function could be further optimized by only processing   ''even''   numbers, with unity being treated as a special case.
<syntaxhighlight lang="rexx">/*REXX program finds and displays N number of anti─primes oranti-primes highly─composite(highly-composite) numbers.*/
parseParse argArg N . /* obtain optional argument from the CL. */
ifIf N=='' | N=="," thenThen N= 20 /* Not specified? Then use the default. */
maxD= 0 /* the maximum number of divisors so far */
saySay '─index─-index- ──anti─prime──--anti-prime--' /* display a title forFor the numbers shown */
#nn= 0 /* the count of anti─primesanti-primes found " " */
Do i=1 For 59 While nn<N /* step through possible numbers by twos */
do once=1 for 1
d=nndivs(i) do i=1 for 59 /* get nn divisors; /*step through possible numbers by twos*/
If d>maxD Then Do d= #divs(i); if d<=maxD then iterate /*get #found divisors;an anti-prime Isnn set new maxD too small? Skip.*/
maxD=d
#= # + 1; maxD= d /*found an anti─prime #; set new minD.*/
nn=nn+1
say center(#, 7) right(i, 10) /*display the index and the anti─prime.*/
Say center(nn,7) if #>=N then leave once right(i,10) /*if wedisplay havethe enoughindex anti─primes,and donethe anti-prime. */
end /*i*/End
End /*i*/
 
Do do ji=60 by 20 While nn<N /* step through possible numbers by 20. */
d=nndivs(i)
d= #divs(j); if d<=maxD then iterate /*get # divisors; Is too small? Skip.*/
If d>maxD Then Do #= # + 1; maxD= d /* found an anti─prime #;anti-prime nn set new minD.maxD */
maxD=d
say center(#, 7) right(j, 10) /*display the index and the anti─prime.*/
nn=nn+1
if #>=N then leave /*if we have enough anti─primes, done. */
saySay center(#nn, 7) right(i, 10) /* display the index and the anti─primeanti-prime. */
end /*j*/
End
end /*once*/
End /*i*/
exit /*stick a fork in it, we're all done. */
Exit #= # + 1; maxD= d /*found anstick anti─primea #;fork in setit, newwe're minDall done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*-----------------------------------------------------------------------------------*/
#divs: procedure; parse arg x 1 y /*X and Y: both set from 1st argument.*/
nndivs: Procedure if x<3 then return x /* compute the number of proper /*handle special cases for onedivisors and two.*/
Parse Arg x
if x==4 then return 3 /* " " " " four. */
If x<2 Then
if x<6 then return 2 /* " " " " three or five*/
Return 1
odd= x // 2 /*check if X is odd or not. */
odd=x//2
if odd then #= 1 /*Odd? Assume Pdivisors count of 1.*/
n=1 else do; #= 3; y= x % 2 /*Even? " /* 1 is a proper divisor " " " 3.*/
Do j=2+odd by 1+odd While j*j<x /* test all endpossible integers /* [↑] start with known num of Pdivs.*/
exit /*stick aup To but excluding sqrt(x) fork in it, we're all done. */
 
If x//j==0 Then do k=3 for x%2-3 by 1+odd while k<y /*for oddj numbersis a divisor,so is x%j skip evens.*/
n=n+2
if x//k==0 then do; #= # + 2 /*if no remainder, then found a divisor*/
End
y= x % k /*bump # Pdivs, calculate limit Y. */
If j*j==x Then /* If x is a square if k>=y then do; #= # - 1; leave; end /*limit?*/
n=n+1 end /* sqrt(x) is a proper divisor ___ */
n=n+1 else if k /*k> x is a proper divisor then leave /*only divide up to x */
Return n</syntaxhighlight>
end /*k*/ /* [↑] this form of DO loop is faster.*/
return #+1 /*bump "proper divisors" to "divisors".*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 20 </tt>}}
<pre>
2,295

edits