Anti-primes: Difference between revisions

Content added Content deleted
(→‎version 1: changed the format of the output from horizontal to vertical.)
m (→‎{{header|REXX}}: elided REXX version 2.)
Line 345: Line 345:


=={{header|REXX}}==
=={{header|REXX}}==
===version 1===
This REXX version is using a modified version of a highly optimized   ''proper divisors''   function.
This REXX version is using a modified version of a highly optimized   ''proper divisors''   function.


Programming note:   although the solution to this Rosetta Code task is trivial, a fair amount of optimization was incorporated into the REXX program to find larger anti─primes (highly─composite numbers).
Programming note:   although the solution to this Rosetta Code task is trivial, a fair amount of optimization was incorporated into the REXX program to find larger anti─primes (highly─composite numbers).

The   #DIVS   function could be further optimized by only processing   ''even''   numbers, with unity being treated as a special case.
<lang rexx>/*REXX program finds N number of anti─primes or highly─composite numbers. */
<lang rexx>/*REXX program finds N number of anti─primes or highly─composite numbers. */
parse arg N . /*obtain optional argument from the CL.*/
parse arg N . /*obtain optional argument from the CL.*/
Line 413: Line 414:
</pre>
</pre>


===version 2===
This REXX version takes advantage of the fact that anti─primes (highly─composite numbers) above 59 can be incremented by 20.
<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=55 /*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 j=1 for 59 while #<N /*step through possible numbers by twos*/
d= #divs(j) /*obtain the number of divisors for K.*/
if d<=maxD then iterate /*Is D ≤ previous div count? Skip it. */
#= # + 1; $= $ j /*found an anti─prime #, add it to list*/
say right(#, 11) j
maxD= d /*use this as the new high─water mark. */
end /*j*/

do j=60 by 20 while #<N /*step through possible numbers by 20. */
d= #divs(j) /*obtain the number of divisors for K.*/
if d<=maxD then iterate /*Is D ≤ previous div count? Skip it. */
#= # + 1; $= $ j /*found an anti─prime #, add it to list*/
say right(#, 11) j
maxD= d /*use this as the new high─water mark. */
end /*j*/
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 k=3 for x%2-3 by 1+odd while k<y /*for odd numbers, skip evens.*/
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=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 55 </tt>}}
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 55 </tt>}}
<pre>
<pre>
─index─ ──anti─prime──
1 1
2 2
1 1
3 4
2 2
4 6
3 4
5 12
4 6
6 24
5 12
7 36
6 24
8 48
7 36
9 60
8 48
10 120
9 60
11 180
10 120
12 240
11 180
13 360
12 240
14 720
13 360
15 840
14 720
16 1260
15 840
17 1680
16 1260
18 2520
17 1680
19 5040
18 2520
20 7560
19 5040
21 10080
20 7560
22 15120
21 10080
23 20160
22 15120
24 25200
23 20160
25 27720
24 25200
26 45360
25 27720
27 50400
26 45360
28 55440
27 50400
29 83160
28 55440
30 110880
29 83160
31 166320
30 110880
32 221760
31 166320
33 277200
32 221760
34 332640
33 277200
35 498960
34 332640
36 554400
35 498960
37 665280
36 554400
38 720720
37 665280
39 1081080
38 720720
40 1441440
39 1081080
41 2162160
40 1441440
42 2882880
41 2162160
43 3603600
42 2882880
44 4324320
43 3603600
45 6486480
44 4324320
46 7207200
45 6486480
47 8648640
46 7207200
48 10810800
47 8648640
49 14414400
48 10810800
50 17297280
49 14414400
51 21621600
50 17297280
52 32432400
51 21621600
53 36756720
52 32432400
54 43243200
53 36756720
55 61261200
54 43243200
55 61261200
</pre>
</pre>