Anti-primes: Difference between revisions

 
(39 intermediate revisions by 13 users not shown)
Line 309:
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN # find some anti-primes: numbers with more divisors than the #
BEGIN # find some anti-primes: numbers with more divisors than the #
# previous numbers #
REF[]INT maxndc number := 10HEAP[ 0001 : 0 ]INT; # table of divisor counts #
INT max divisors := 0;
INT a count := 0;
# construct a table of the divisor counts #
[ 1 : max number ]INT ndc; FOR in FROMWHILE 1a TOcount UPB< ndc20 DO ndc[ i ] := 1 OD;
FOR i FROM 2 TOIF n > UPB ndc DOTHEN
FOR j FROM i BY# ineed TOa UPBbigger ndctable DOof ndc[divisor jcounts ] +:= 1 OD #
ndc := HEAP[ 1 : UPB ndc + 5 000 ]INT;
OD;
FOR i FROM 1 TO UPB ndc DO ndc[ i ] := 1 OD;
# show the numbers with more divisors than their predecessors #
FOR i FROM 2 TO UPB ndc DO
INT a count := 0;
FOR j FROM i BY i TO UPB ndc WHILEDO andc[ countj <] 20+:= DO1 OD
INT divisor count = ndc[ i ];OD
FI;
IF divisor count > max divisors THEN
IF ndc[ n ] print(> (max "divisors ", whole( i, 0 ) ) );THEN
maxprint( divisors( :=" divisor", countwhole( n, 0 ) ) );
max divisors := ndc[ n ];
a count +:= 1
FI
OD;
print( ( newline ) )
END
END</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
Line 621 ⟶ 624:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256vbnet">
Dim Results(20)
Candidate = 1
Line 693 ⟶ 696:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
==={{header|Gambas}}===
<syntaxhighlight lang="gambas">Function count_divisors(n As Integer) As Integer
 
Dim i, count As Integer
If n < 2 Then Return 1
count = 2
For i = 2 To n / 2
If Not (n Mod i) Then Inc count
Next
Return count
 
End Function
 
Public Sub Main()
 
Dim count, max_divisors, n, d As Integer
Print "Los primeros 20 anti-primos son:"
While (count < 20)
Inc n
d = count_divisors(n)
If d > max_divisors Then
Print n; " ";
max_divisors = d
Inc count
End If
Wend
End</syntaxhighlight>
{{out}}
<pre>The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 </pre>
 
==={{header|GW-BASIC}}===
Line 754 ⟶ 791:
5040
7560</pre>
 
==={{header|Palo Alto Tiny BASIC}}===
{{trans|Tiny BASIC}}
<syntaxhighlight lang="basic">
100 REM ANTI-PRIMES
110 LET N=1,H=0
120 PRINT "THE FIRST 20 ANTI-PRIMES ARE:"
130 FOR A=1 TO 20
140 GOSUB 300
150 LET H=F
160 PRINT N
170 LET N=N+1
180 NEXT A
190 STOP
290 REM SEARCH NEXT ANTI-PRIME
300 GOSUB 400
310 IF F>H RETURN
320 LET N=N+1
330 GOTO 300
390 REM COUNT DIVISORS
400 LET F=1
410 IF N>1 LET F=2
420 LET C=2
430 IF C*C>=N GOTO 470
440 IF (N/C)*C=N LET F=F+2
450 LET C=C+1
460 GOTO 430
470 IF C*C=N F=F+1
480 RETURN
</syntaxhighlight>
{{out}}
<pre>
THE FIRST 20 ANTI-PRIMES ARE:
1
2
4
6
12
24
36
48
60
120
180
240
360
720
840
1260
1680
2520
5040
7560
</pre>
 
==={{header|PureBasic}}===
Line 787 ⟶ 878:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">MaxAntiPrime = 20
n = 0
MaxDivisors = 0
AntiPrimeCount = 0
PRINT "The first 20 anti-primes are: "
WHILE AntiPrimeCount < MaxAntiPrime
n = n + 1
Divisors = DivisorCount(n)
IF Divisors > MaxDivisors THEN
PRINT n;
MaxDivisors = Divisors
AntiPrimeCount = AntiPrimeCount + 1
END IF
WEND
END
 
FUNCTION DivisorCount (v)
total = 1
n = v
WHILE n MOD 2 = 0
total = total + 1
n = n \ 2
WEND
p = 3
WHILE (p * p) <= n
count = 1
WHILE n MOD p = 0
count = count + 1
n = n \ p
WEND
p = p + 2
total = total * count
WEND
IF n > 1 THEN total = total * 2
DivisorCount = total
END FUNCTION</syntaxhighlight>
{{out}}
<pre>The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 </pre>
 
==={{header|QuickBASIC}}===
{{trans|ALGOL W}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">
' Anti-primes
Line 832 ⟶ 968:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="lb">MaxAntiPrime = 20
n = 0
MaxDivisors = 0
AntiPrimeCount = 0
print "The first 20 anti-primes are: "
while AntiPrimeCount < MaxAntiPrime
n = n +1
Divisors = DivisorCount(n)
if Divisors > MaxDivisors then
print n; " ";
MaxDivisors = Divisors
AntiPrimeCount = AntiPrimeCount +1
end if
wend
end
 
function DivisorCount(v)
total = 1
n = v
while n mod 2 = 0
total = total +1
n = int(n / 2)
wend
p = 3
while (p * p) <= n
count = 1
while n mod p = 0
count = count +1
n = int(n / p)
wend
p = p +2
total = total * count
wend
if n > 1 then total = total *2
DivisorCount = total
end function</syntaxhighlight>
{{out}}
<pre>The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 </pre>
 
==={{header|Tiny BASIC}}===
{{works with|TinyBasic}}
<syntaxhighlight lang="basic">100 LETREM A=0Anti-primes
101110 LET NA=10
102120 LET HN=01
130 LET H=0
103 PRINT "The first 20 anti-primes are:"
140 PRINT "The first 20 anti-primes are:"
105 GOSUB 150
150 GOSUB 300
106 LET H=F
107160 LET AH=A+1F
170 LET A=A+1
108 PRINT N
180 PRINT N
109 LET N=N+1
190 LET N=N+1
110 IF A<20 THEN GOTO 105
200 IF A<20 THEN GOTO 150
111 END
210 END
150 GOSUB 200
290 REM Search next anti-prime
151 IF F>H THEN RETURN
300 GOSUB 400
152 LET N=N+1
310 IF F>H THEN RETURN
153 GOTO 150
200320 LET FN=0N+1
330 GOTO 300
201 LET C=1
390 REM Count divisors
205 IF N/C*C=N THEN LET F=F+1
206400 LET CF=C+1
207410 IF C<=N>1 THEN GOTOLET 205F=2
420 LET C=2
208 RETURN
430 IF C*C>=N THEN GOTO 470
440 IF (N/C)*C=N THEN LET F=F+2
450 LET C=C+1
460 GOTO 430
470 IF C*C=N THEN LET F=F+1
480 RETURN
</syntaxhighlight>
{{out}}
Line 958 ⟶ 1,143:
 
==={{header|Yabasic}}===
{{Output?|Yabasic}}
{{trans|AWK}}
<syntaxhighlight lang="yabasic">print "The first 20 anti-primes are:"
Line 984 ⟶ 1,168:
return count
end sub</syntaxhighlight>
{{out}}
<pre>The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560</pre>
 
{{trans|Lua}}
Line 1,017 ⟶ 1,204:
print mid$(antiprimes$(20), 3)
print "Done."</syntaxhighlight>
{{out}}
<pre>The first 20 antiprimes:
1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560
Done.</pre>
 
 
=={{header|BCPL}}==
Line 1,403 ⟶ 1,595:
<pre>The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560</pre>
 
=={{header|Dart}}==
{{trans|C++}}
<syntaxhighlight lang="dart">int countDivisors(int n) {
if (n < 2) return 1;
int count = 2; // 1 and n
for (int i = 2; i <= n / 2; ++i) {
if (n % i == 0) ++count;
}
return count;
}
 
void main() {
int maxDiv = 0, count = 0;
print("The first 20 anti-primes are:");
for (int n = 1; count < 20; ++n) {
int d = countDivisors(n);
if (d > maxDiv) {
print("$n ");
maxDiv = d;
count++;
}
}
print("");
}</syntaxhighlight>
 
=={{header|Delphi}}==
See [[#Pascal]].
=={{header|EasyLang}}==
{{trans|FutureBasic}}
<syntaxhighlight lang=easylang>
func divcnt v .
n = v
tot = 1
p = 2
while p <= sqrt n
cnt = 1
while n mod p = 0
cnt += 1
n = n div p
.
p += 1
tot *= cnt
.
if n > 1
tot *= 2
.
return tot
.
while count < 20
n += 1
divs = divcnt n
if divs > max
print n
max = divs
count += 1
.
.
</syntaxhighlight>
 
=={{header|Elixir}}==
{{trans|Erlang}}<syntaxhighlight lang="elixir">defmodule AntiPrimes do
Line 1,441 ⟶ 1,691:
The first 20 anti-primes are [1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560]
</pre>
 
=={{header|EMal}}==
{{trans|Java}}
<syntaxhighlight lang="emal">
fun countDivisors = int by int n
if n < 2 do return 1 end
int count = 2
for int i = 2; i <= n / 2; ++i
if n % i == 0 do ++count end
end
return count
end
int maxDiv = 0
int count = 0
writeLine("The first 20 anti-primes are:")
for int n = 1; count < 20; ++n
int d = countDivisors(n)
if d <= maxDiv do continue end # never nester version
write(n + " ")
maxDiv = d
++count
end
writeLine()
</syntaxhighlight>
{{out}}
<pre>
The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">divcount(N) -> divcount(N, 1, 0).
Line 1,898 ⟶ 2,178:
<pre>
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn DivisorCount( v as long ) as long
long total = 1, n = v, p, count
while ( n mod 2 ) == 0
total++
n = int( n / 2 )
wend
p = 3
while ( p * p ) <= n
count = 1
while ( n mod p ) == 0
count++
n = int( n / p )
wend
p = p + 2
total = total * count
wend
if n > 1 then total = total * 2
end fn = total
 
void local fn AntiPrimes( howMany as long )
long n = 0, count = 0, divisors, max_divisors = 0
printf @"The first %ld anti-primes are:", howMany
while ( count < howMany )
n++
divisors = fn DivisorCount( n )
if ( divisors > max_divisors )
printf @"%ld \b", n
max_divisors = divisors
count++
end if
wend
end fn
 
fn AntiPrimes( 20 )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
Line 1,938 ⟶ 2,266:
The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
=={{header|Grain}}==
<syntaxhighlight lang="haskell">
import File from "sys/file"
let mut maxDiv = 0
let mut count = 0
let numaprimes = 20
let countDivisors = n => {
if (n < 1) {
1
} else {
let mut count = 2
let mut target = n / 2
for (let mut i = 1; i <= target; i += 1) {
if (n % i == 0) {
count += 1
} else {
void
}
}
count
}
}
print("\nThe first 20 anti-primes are: ")
let mut d = 0
for (let mut j = 1; count < numaprimes; j += 1) {
d = countDivisors(j)
if (d > maxDiv) {
File.fdWrite(File.stdout, Pervasives.toString(j))
File.fdWrite(File.stdout, " ")
maxDiv = d
count += 1
}
}
</syntaxhighlight>
{{out}}
<pre>
The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
Line 2,252 ⟶ 2,620:
The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
=={{header|Lambdatalk}}==
 
==1) lambdatalk only==
<syntaxhighlight lang="scheme">
{def factors
{def factors.filter
{lambda {:n :a :i}
{if {= {% :n :i} 0}
then {A.addlast! :i :a}
else}}}
{lambda {:n}
{S.last
{S.map {factors.filter :n {A.new}}
{S.serie 1 :n}}}}}
-> factors
 
{def antiprimes
{def antiprimes.filter
{lambda {:ap :max :i}
{let { {:ap :ap} {:max :max} {:i :i}
{:len {A.length {factors :i}}}
} {if {> :len {A.get 0 :max}}
then {A.addlast! :i :ap}
{A.set! 0 :len :max}
else} }}}
{lambda {:n}
{S.first
{S.map {antiprimes.filter {A.new 1} {A.new 1}}
{S.serie 1 :n}}}}}
-> antiprimes
 
{antiprimes 8000} // 8000 choosen manually to reach 20 antiprimes
-> [1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560]
// in 105400ms on my iPad
 
</syntaxhighlight>
 
==2) using javascript==
Lambdatalk can call javascript code, here simply copying the code written in the javascript entry.
 
<syntaxhighlight lang="scheme">
1) building the interface to the javascript function.
 
{script
LAMBDATALK.DICT['jsgenerateAntiprimes'] = function() {
function factors(n) {
var factors = [];
for (var i = 1; i <= n; i++) {
if (n % i == 0) {
factors.push(i);
}
}
return factors;
}
 
function generateAntiprimes(n) {
var antiprimes = [];
var maxFactors = 0;
for (var i = 1; antiprimes.length < n; i++) {
var ifactors = factors(i);
if (ifactors.length > maxFactors) {
antiprimes.push(i);
maxFactors = ifactors.length;
}
}
return antiprimes;
}
return generateAntiprimes( arguments[0].trim() )
};
}
 
2) and using it in the wiki page as a builtin primitive
 
{jsgenerateAntiprimes 20}
->
1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560 // in 100ms
 
</syntaxhighlight>
 
=={{header|langur}}==
{{trans|D}}
<syntaxhighlight lang="langur">val .countDivisors = fn(.n) {
if .n < 2: return 1
for[=2] .i = 2; .i <= .n\2; .i += 1 {
if .n div .i : _for += 1
}
}
 
writeln "The first 20 anti-primes are:"
var .maxDiv, .count = 0, 0
for .n = 1; .count < 20; .n += 1 {
val .d = .countDivisors(.n)
if .d > .maxDiv {
write .n, " "
.maxDiv = .d
.count += 1
}
}
writeln()
</syntaxhighlight>
 
{{out}}
<pre>1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
=={{header|Lua}}==
 
===Counting the factors using modulo===
<syntaxhighlight lang="lua">-- First 20 antiprimes.
 
Line 2,315 ⟶ 2,790:
7560
Done.</pre>
 
===Using a table of divisor counts===
 
<syntaxhighlight lang="lua">
-- Find the first 20 antiprimes.
 
-- returns a table of the first goal antiprimes
function antiprimes(goal)
local maxNumber = 0
local ndc = {} -- table of divisor counts - initially empty
local list, number, mostFactors = {}, 1, 0
while #list < goal do
if number > #ndc then
-- need a bigger table of divisor counts
maxNumber = maxNumber + 5000
ndc = {}
for i = 1, maxNumber do ndc[ i ] = 1 end
for i = 2, maxNumber do
for j = i, maxNumber, i do ndc[ j ] = ndc[ j ] + 1 end
end
end
local factors = ndc[ number ]
if factors > mostFactors then
table.insert( list, number )
mostFactors = factors
end
number = number + 1
end
return list
end
 
-- display the antiprimes
oo.write( table.concat( antiprimes( 20 ), " " ) )
</syntaxhighlight>.
 
{{out}}
<pre>
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
=={{header|Maple}}==
Line 2,353 ⟶ 2,867:
{{out}}
<pre>{1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560,10080,15120,20160,25200,27720}</pre>
 
=={{header|MiniScript}}==
{{Trans|Lua|Using a table of divisor counts}}
<syntaxhighlight lang="miniscript">
// Find the first 20 antiprimes.
 
// returns a table of the first goal antiprimes
antiprimes = function(goal)
maxNumber = 0
ndc = [] // table of divisor counts - initially empty
list = [0] * goal; number = 1; mostFactors = 0
aCount = 0
while aCount < goal
if number > maxNumber then
// need a bigger table of divisor counts
maxNumber = maxNumber + 5000
ndc = [1] * ( maxNumber + 1 )
ndc[ 0 ] = 0
for i in range( 2, maxNumber )
for j in range( i, maxNumber, i )
ndc[ j ] = ndc[ j ] + 1
end for
end for
end if
factors = ndc[ number ]
if factors > mostFactors then
list[ aCount ] = number
mostFactors = factors
aCount = aCount + 1
end if
number = number + 1
end while
return list
end function
 
// display the antiprimes
print antiprimes( 20 ).join( " " )
</syntaxhighlight>
{{out}}
<pre>
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
=={{header|Modula-2}}==
Line 2,595 ⟶ 3,151:
<pre>
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
=={{header|Odin}}==
Anti-Primes
Odin Build: dev-2023-07-nightly:3072479c
<syntaxhighlight lang="go">
package antiprimes
import "core:fmt"
 
main :: proc() {
AntiPrimeCount, MaxDivisors, Divisors, n: u64
MaxAntiPrime: u64 = 20
fmt.print("\nFirst 20 anti-primes\n")
fmt.println("--------------------")
for (AntiPrimeCount < MaxAntiPrime) {
n += 1
Divisors = DivisorCount(n)
if Divisors > MaxDivisors {
fmt.print(n, " ")
MaxDivisors = Divisors
AntiPrimeCount += 1
}
}
}
 
DivisorCount :: proc(v: u64) -> u64 {
total: u64 = 1
a := v
if a == 0 {
return 0
}
for a % 2 == 0 {
total += 1
a /= 2
}
for p: u64 = 3; p * p <= a; p += 2 {
count: u64 = 1
for a % p == 0 {
count += 1
a /= p
}
total *= count
}
if a > 1 {
total *= 2
}
return total
}
</syntaxhighlight>
{{out}}
<pre>
First 20 anti-primes
--------------------
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
Line 2,847 ⟶ 3,457:
5040
7560</pre>
 
=={{header|PL/0}}==
{{trans|Tiny BASIC}}
<syntaxhighlight lang="pascal">
var i, n, maxdivcnt, divcnt;
 
procedure countdivs;
var p;
begin
divcnt := 1;
if n > 1 then divcnt := 2;
p := 2;
while p * p < n do
begin
if (n / p) * p = n then divcnt := divcnt + 2;
p := p + 1
end;
if p * p = n then divcnt := divcnt + 1
end;
 
procedure searchnext;
begin
call countdivs;
while divcnt <= maxdivcnt do
begin
n := n + 1;
call countdivs
end
end;
 
begin
i := 1; n := 1; maxdivcnt := 0;
while i <= 20 do
begin
call searchnext;
maxdivcnt := divcnt;
i := i + 1;
! n;
n := n + 1
end
end.
</syntaxhighlight>
{{out}}
<pre>
1
2
4
6
12
24
36
48
60
120
180
240
360
720
840
1260
1680
2520
5040
7560
</pre>
 
=={{header|PL/I}}==
Line 3,171 ⟶ 3,846:
 
The &nbsp; #DIVS &nbsp; function could be further optimized by only processing &nbsp; ''even'' &nbsp; 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. */
Say center(nn,7) right(i,10) /* display the index and the anti-prime. */
end /*j*/
End
end /*once*/
End /*i*/
exit /*stick a fork in it, we're all done. */
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.*/
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.*/
/* up To but excluding sqrt(x) */
 
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>
Line 3,341 ⟶ 4,016:
 
=={{header|Ring}}==
===Counting the divisors using modulo===
<syntaxhighlight lang="ring">
# Project : Anti-primes
Line 3,392 ⟶ 4,068:
 
done...
</pre>
 
===Counting the divisors using a table===
 
<syntaxhighlight lang="ring">
# find the first 20 antiprimes
# - numbers woth more divisors than the previous numbers
 
numberOfDivisorCounts = 0
maxDivisor = 0
num = 0
n = 0
result = list(20)
while num < 20
n += 1
if n > numberOfDivisorCounts
# need a bigger table of divisor counts
numberOfDivisorCounts += 5000
ndc = list(numberOfDivisorCounts)
for i = 1 to numberOfDivisorCounts
ndc[ i ] = 1
next
for i = 2 to numberOfDivisorCounts
j = i
while j <= numberOfDivisorCounts
ndc[ j ] = ndc[ j ] + 1
j += i
end
next
ok
div = ndc[ n ]
if (div > maxDivisor)
maxDivisor = div
num += 1
result[num] = n
ok
end
see result[1]
for n = 2 to len(result)
see " " + string(result[n])
next
</syntaxhighlight>
 
{{out}}
<pre>
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
Line 3,398 ⟶ 4,120:
{{works with|Halcyon Calc|4.2.7}}
≪ → nb
≪ 1 1 nb 2 / FOR j
1 nb 2 / '''FOR''' j
nb j MOD NOT +
'''NEXT'''
≫ ≫ ‘<span style="color:blue">NDIV</span>’ STO
‘NDIV’ STO
≪ 1 - → items
≪ { } (2,0)
'''DO'''
DUP RE <span style="color:blue">NDIV</span>
'''IF''' OVER IM OVER < '''THEN'''
SWAP RE SWAP R→C
SWAP OVER RE + SWAP
'''ELSE''' DROP '''END'''
1 DROP+
'''UNTIL''' OVER SIZE items > '''END'''
1 +
UNTIL OVER SIZE items > END
DROP
≫ ≫ ‘<span style="color:blue">ANTIP</span>’ STO
 
15 <span style="color:blue">ANTIP</span>
‘ANTIP’ STO
15 ANTIP
{{out}}
<pre>
Line 3,428 ⟶ 4,147:
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
{{works with|Halcyon Calc|4.2.7}}
def num_divisors(n)
n.prime_division.inject(1){|prod, (_p,n)| prod *= (n + 1) }
end
 
Line 3,748 ⟶ 4,465:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
System.print("The first 20 anti-primes are:")
889

edits