Anti-primes: Difference between revisions
Content added Content deleted
(→{{header|zkl}}: added code) |
(→{{header|REXX}}: added the REXX computer programming language.) |
||
Line 310: | Line 310: | ||
{{out}} |
{{out}} |
||
<pre>[1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560]</pre> |
<pre>[1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560]</pre> |
||
=={{header|REXX}}== |
|||
This REXX version is using a modified version of a highly optimized ''proper divisors'' function. |
|||
<lang rexx>/*REXX program finds 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.*/ |
|||
maxD= 0 /*the maximum number of divisors so far*/ |
|||
#= 0 /*the count of anti─primes found " " */ |
|||
$= /*the list of anti─primes found " " */ |
|||
do k=1 until #==N; d= #divs(k) /*obtain the number of divisors for K.*/ |
|||
if d<=maxD then iterate /*Is D ≤ previous div count? Skip it. */ |
|||
#= # + 1; $= $ k /*found an anti1prime #, add it to list*/ |
|||
maxD= d /*use this as the new high─water mark. */ |
|||
end /*k*/ |
|||
say |
|||
say 'The first ' N " anti─primes (highly─composite numbers) are:" |
|||
say strip($) |
|||
exit /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
#divs: procedure; parse arg x 1 y /*X and Y: both set from 1st argument.*/ |
|||
if x<3 then return x /*handle special case for one and two. */ |
|||
if x==4 then return 3 /* " " " " four. */ |
|||
if x<6 then return 2 /* " " " " three or five*/ |
|||
odd= x // 2 /*check if X is odd or not. */ |
|||
if odd then do; #= 1; end /*Odd? Assume Pdivisors count of 1.*/ |
|||
else do; #= 3; y= x%2; end /*Even? " " " " 3.*/ |
|||
/* [↑] start with known num of Pdivs.*/ |
|||
do j=3 for x%2-3 by 1+odd while j<y /*for odd numbers, skip evens.*/ |
|||
if x//j==0 then do /*if no remainder, then found a divisor*/ |
|||
#=#+2; y=x%j /*bump # Pdivs, calculate limit Y. */ |
|||
if j>=y then do; #= #-1; leave; end /*limit?*/ |
|||
end /* ___ */ |
|||
else if j*j>x then leave /*only divide up to √ x */ |
|||
end /*j*/ /* [↑] this form of DO loop is faster.*/ |
|||
return #+1 /*bump "proper divisors" to "divisors".*/</lang> |
|||
{{out|output|text= when using the default input of: <tt> 20 </tt>}} |
|||
<pre> |
|||
The first 20 anti─primes (highly─composite numbers) are: |
|||
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 |
|||
</pre> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |