Jump to content

Anti-primes: Difference between revisions

m
→‎{{header|REXX}}: elided REXX version 2.
(→‎version 1: changed the format of the output from horizontal to vertical.)
m (→‎{{header|REXX}}: elided REXX version 2.)
Line 345:
 
=={{header|REXX}}==
===version 1===
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).
 
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. */
parse arg N . /*obtain optional argument from the CL.*/
Line 413 ⟶ 414:
</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>}}
<pre>
─index─ ──anti─prime──
1 1
1 2 2 1
2 3 4 2
3 4 6 4
4 5 12 6
5 6 24 12
6 7 36 24
7 8 48 36
8 9 60 48
9 10 120 60
10 11 180 120
11 12 240 180
12 13 360 240
13 14 720 360
14 15 840 720
15 16 1260 840
16 17 1680 1260
17 18 2520 1680
18 19 5040 2520
19 20 7560 5040
20 21 10080 7560
21 22 15120 10080
22 23 20160 15120
23 24 25200 20160
24 25 27720 25200
25 26 45360 27720
26 27 50400 45360
27 28 55440 50400
28 29 83160 55440
29 30 110880 83160
30 31 166320110880
31 32 221760166320
32 33 277200221760
33 34 332640277200
34 35 498960332640
35 36 554400498960
36 37 665280554400
37 38 720720665280
38 39 1081080720720
39 40 14414401081080
40 41 21621601441440
41 42 28828802162160
42 43 36036002882880
43 44 43243203603600
44 45 64864804324320
45 46 72072006486480
46 47 86486407207200
47 48 108108008648640
48 49 1441440010810800
49 50 1729728014414400
50 51 2162160017297280
51 52 3243240021621600
52 53 3675672032432400
53 54 4324320036756720
54 55 6126120043243200
55 61261200
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.