Anti-primes
You are encouraged to solve this task according to the task description, using any language you may know.
The anti-primes (or highly composite numbers, sequence A002182 in the OEIS) are the natural numbers with more factors than any smaller than itself.
- Task
Generate and show here, the first twenty anti-primes.
- Related tasks
11l
V max_divisors = 0
V c = 0
V n = 1
L
V divisors = 1
L(i) 1 .. n I/ 2
I n % i == 0
divisors++
I divisors > max_divisors
max_divisors = divisors
print(n, end' ‘ ’)
c++
I c == 20
L.break
n++
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
8086 Assembly
puts: equ 9 ; MS-DOS print string syscall
amount: equ 20 ; Amount of antiprimes to find
cpu 8086
org 100h
xor si,si ; SI = current number
xor cx,cx ; CH = max # of factors, CL = # of antiprimes
cand: inc si
mov di,si ; DI = maximum factor to test
shr di,1
mov bp,1 ; BP = current candidate
xor bl,bl ; BL = factor count
.test: mov ax,si ; Test current candidate
xor dx,dx
div bp
test dx,dx ; Evenly divisible?
jnz .next
inc bx ; Then increment factors
.next: inc bp ; Next possible factor
cmp bp,si ; Are we there yet?
jbe .test ; If not, try next factor
cmp bl,ch ; Is it an antiprime?
jbe cand ; If not, next candidate
inc cx ; If so, increment the amount of antiprimes seen
mov ch,bl ; Update maximum amount of factors
mov bx,nbuf ; Convert current number to ASCII
mov ax,si
mov di,10
digit: xor dx,dx ; Extract a digit
div di
add dl,'0' ; Add ASCII 0
dec bx
mov [bx],dl ; Store it
test ax,ax ; Any more digits?
jnz digit ; If so, get next digit
mov dx,bx
mov ah,puts
int 21h ; Print using MS-DOS
cmp cl,amount ; Do we need any more antiprimes?
jb cand ; If so, find the next one
ret ; Otherwise, back to DOS
db '.....' ; Placeholder for decimal output
nbuf: db ' $'
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
AArch64 Assembly
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program antiprime64.s */
/************************************/
/* Constantes */
/************************************/
.include "../includeConstantesARM64.inc"
.equ NMAXI, 20
.equ MAXLINE, 5
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResult: .asciz " @ "
szCarriageReturn: .asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x3,qNMaxi // load limit
mov x5,#0 // maxi
mov x6,#0 // result counter
mov x7,#0 // display counter
mov x4,#1 // number begin
1:
mov x0,x4 // number
bl decFactor // compute number factors
cmp x0,x5 // maxi ?
cinc x4,x4,le // no -> increment indice
//addle x4,x4,#1 // no -> increment indice
ble 1b // and loop
mov x5,x0
mov x0,x4
bl displayResult
add x7,x7,#1 // increment display counter
cmp x7,#MAXLINE // line maxi ?
blt 2f
mov x7,#0
ldr x0,qAdrszCarriageReturn
bl affichageMess // display message
2:
add x6,x6,#1 // increment result counter
add x4,x4,#1 // increment number
cmp x6,x3 // end ?
blt 1b
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qNMaxi: .quad NMAXI
/***************************************************/
/* display message number */
/***************************************************/
/* x0 contains number 1 */
/* x1 contains number 2 */
displayResult:
stp x1,lr,[sp,-16]! // save registers
ldr x1,qAdrsZoneConv
bl conversion10 // call décimal conversion
ldr x0,qAdrsMessResult
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
bl affichageMess // display message
ldp x1,lr,[sp],16 // restaur registers
ret
qAdrsMessResult: .quad sMessResult
qAdrsZoneConv: .quad sZoneConv
/***************************************************/
/* compute factors sum */
/***************************************************/
/* x0 contains the number */
decFactor:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
mov x5,#0 // init number factors
mov x4,x0 // save number
mov x1,#1 // start factor -> divisor
1:
mov x0,x4 // dividende
udiv x2,x0,x1
msub x3,x2,x1,x0
cmp x1,x2 // divisor > quotient ?
bgt 3f
cmp x3,#0 // remainder = 0 ?
bne 2f
add x5,x5,#1 // increment counter factors
cmp x1,x2 // divisor = quotient ?
beq 3f // yes -> end
add x5,x5,#1 // no -> increment counter factors
2:
add x1,x1,#1 // increment factor
b 1b // and loop
3:
mov x0,x5 // return counter
ldp x4,x5,[sp],16 // restaur registers
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../includeARM64.inc"
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Action!
BYTE FUNC CountDivisors(INT a)
INT i
BYTE prod,count
prod=1 count=0
WHILE a MOD 2=0
DO
count==+1
a==/2
OD
prod==*(1+count)
i=3
WHILE i*i<=a
DO
count=0
WHILE a MOD i=0
DO
count==+1
a==/i
OD
prod==*(1+count)
i==+2
OD
IF a>2 THEN
prod==*2
FI
RETURN (prod)
PROC Main()
BYTE toFind=[20],found=[0],count,max=[0]
INT i=[1]
PrintF("The first %B Anti-primes are:%E",toFind)
WHILE found<toFind
DO
count=CountDivisors(i)
IF count>max THEN
max=count
found==+1
PrintI(i)
IF found<toFind THEN
Print(", ")
FI
FI
i==+1
OD
RETURN
- Output:
Screenshot from Atari 8-bit computer
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
Ada
with Ada.Text_IO; use Ada.Text_IO;
procedure Antiprimes is
function Count_Divisors (N : Integer) return Integer is
Count : Integer := 1;
begin
for i in 1 .. N / 2 loop
if N mod i = 0 then
Count := Count + 1;
end if;
end loop;
return Count;
end Count_Divisors;
Results : array (1 .. 20) of Integer;
Candidate : Integer := 1;
Divisors : Integer;
Max_Divisors : Integer := 0;
begin
for i in Results'Range loop
loop
Divisors := Count_Divisors (Candidate);
if Max_Divisors < Divisors then
Results (i) := Candidate;
Max_Divisors := Divisors;
exit;
end if;
Candidate := Candidate + 1;
end loop;
end loop;
Put_Line ("The first 20 anti-primes are:");
for I in Results'Range loop
Put (Integer'Image (Results (I)));
end loop;
New_Line;
end Antiprimes;
- Output:
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
ALGOL 68
BEGIN # find some anti-primes: numbers with more divisors than the #
# previous numbers #
REF[]INT ndc := HEAP[ 1 : 0 ]INT; # table of divisor counts #
INT max divisors := 0;
INT a count := 0;
FOR n WHILE a count < 20 DO
IF n > UPB ndc THEN
# need a bigger table of divisor counts #
ndc := HEAP[ 1 : UPB ndc + 5 000 ]INT;
FOR i FROM 1 TO UPB ndc DO ndc[ i ] := 1 OD;
FOR i FROM 2 TO UPB ndc DO
FOR j FROM i BY i TO UPB ndc DO ndc[ j ] +:= 1 OD
OD
FI;
IF ndc[ n ] > max divisors THEN
print( ( " ", whole( n, 0 ) ) );
max divisors := ndc[ n ];
a count +:= 1
FI
OD;
print( ( newline ) )
END
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
ALGOL W
begin
% find some anti-primes - numbers with more factors than the numbers %
% smaller than them %
% calculates the number of divisors of v %
integer procedure divisor_count( integer value v ) ; begin
integer total, n, p;
total := 1; n := v;
while not odd( n ) do begin
total := total + 1;
n := n div 2
end while_not_odd_n ;
p := 3;
while ( p * p ) <= n do begin
integer count;
count := 1;
while n rem p = 0 do begin
count := count + 1;
n := n div p
end while_n_rem_p_eq_0 ;
p := p + 2;
total := total * count
end while_p_x_p_le_n ;
if n > 1 then total := total * 2;
total
end divisor_count ;
begin
integer maxAntiPrime, antiPrimeCount, maxDivisors, n;
maxAntiPrime := 20;
n := maxDivisors := antiPrimeCount := 0;
while antiPrimeCount < maxAntiPrime do begin
integer divisors;
n := n + 1;
divisors := divisor_count( n );
if divisors > maxDivisors then begin
writeon( i_w := 1, s_w := 0, " ", n );
maxDivisors := divisors;
antiPrimeCount := antiPrimeCount + 1
end if_have_an_anti_prime
end while_antiPrimeCoiunt_lt_maxAntiPrime
end
end.
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
AppleScript
on factorCount(n)
set counter to 0
set sqrt to n ^ 0.5
set limit to sqrt div 1
if (limit = sqrt) then
set counter to counter + 1
set limit to limit - 1
end if
repeat with i from limit to 1 by -1
if (n mod i is 0) then set counter to counter + 2
end repeat
return counter
end factorCount
on antiprimes(howMany)
set output to {}
set mostFactorsSoFar to 0
set n to 0
repeat until ((count output) = howMany)
set n to n + 1
tell (factorCount(n))
if (it > mostFactorsSoFar) then
set end of output to n
set mostFactorsSoFar to it
end if
end tell
end repeat
return output
end antiprimes
antiprimes(20)
- Output:
{1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560}
APL
Works in Dyalog APL
f←{⍸≠⌈\(⍴∘∪⊢∨⍳)¨⍳⍵}
- Output:
f 8000 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
ARM Assembly
/* ARM assembly Raspberry PI or android with termux */
/* program antiprime.s */
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
.equ NMAXI, 20
.equ MAXLINE, 5
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResult: .asciz " @ "
szCarriageReturn: .asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
ldr r3,iNMaxi @ load limit
mov r5,#0 @ maxi
mov r6,#0 @ result counter
mov r7,#0 @ display counter
mov r4,#1 @ number begin
1:
mov r0,r4 @ number
bl decFactor @ compute number factors
cmp r0,r5 @ maxi ?
addle r4,r4,#1 @ no -> increment indice
ble 1b @ and loop
mov r5,r0
mov r0,r4
bl displayResult
add r7,r7,#1 @ increment display counter
cmp r7,#MAXLINE @ line maxi ?
blt 2f
mov r7,#0
ldr r0,iAdrszCarriageReturn
bl affichageMess @ display message
2:
add r6,r6,#1 @ increment result counter
add r4,r4,#1 @ increment number
cmp r6,r3 @ end ?
blt 1b
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iNMaxi: .int NMAXI
/***************************************************/
/* display message number */
/***************************************************/
/* r0 contains number 1 */
/* r1 contains number 2 */
displayResult:
push {r1,lr} @ save registers
ldr r1,iAdrsZoneConv
bl conversion10 @ call décimal conversion
ldr r0,iAdrsMessResult
ldr r1,iAdrsZoneConv @ insert conversion in message
bl strInsertAtCharInc
bl affichageMess @ display message
pop {r1,pc} @ restaur des registres
iAdrsMessResult: .int sMessResult
iAdrsZoneConv: .int sZoneConv
/***************************************************/
/* compute factors sum */
/***************************************************/
/* r0 contains the number */
decFactor:
push {r1-r5,lr} @ save registers
mov r5,#0 @ init number factors
mov r4,r0 @ save number
mov r1,#1 @ start factor -> divisor
1:
mov r0,r4 @ dividende
bl division
cmp r1,r2 @ divisor > quotient ?
bgt 3f
cmp r3,#0 @ remainder = 0 ?
bne 2f
add r5,r5,#1 @ increment counter factors
cmp r1,r2 @ divisor = quotient ?
beq 3f @ yes -> end
add r5,r5,#1 @ no -> increment counter factors
2:
add r1,r1,#1 @ increment factor
b 1b @ and loop
3:
mov r0,r5 @ return counter
pop {r1-r5,pc} @ restaur registers
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Arturo
found: 0
i: 1
maxDiv: 0
while [found<20][
fac: size factors i
if fac > maxDiv [
print i
maxDiv: fac
found: found + 1
]
i: i + 1
]
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
AWK
# syntax: GAWK -f ANTI-PRIMES.AWK
BEGIN {
print("The first 20 anti-primes are:")
while (count < 20) {
d = count_divisors(++n)
if (d > max_divisors) {
printf("%d ",n)
max_divisors = d
count++
}
}
printf("\n")
exit(0)
}
function count_divisors(n, count,i) {
if (n < 2) {
return(1)
}
count = 2
for (i=2; i<=n/2; i++) {
if (n % i == 0) {
count++
}
}
return(count)
}
- Output:
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
BASIC
BASIC256
Dim Results(20)
Candidate = 1
max_divisors = 0
Print "Los primeros 20 anti-primos son:"
For j = 0 To 19
Do
divisors = count_divisors(Candidate)
If max_divisors < divisors Then
Results[j] = Candidate
max_divisors = divisors
Exit Do
End If
Candidate += 1
Until false
Print Results[j];" ";
Next j
Function count_divisors(n)
cont = 1
For i = 1 To n/2
If (n % i) = 0 Then cont += 1
Next i
count_divisors = cont
End Function
- Output:
Los primeros 20 anti-primos son: 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
FreeBASIC
' convertido desde Ada
Declare Function count_divisors(n As Integer) As Integer
Dim As Integer max_divisors, divisors, results(1 To 20), candidate, j
candidate = 1
Function count_divisors(n As Integer) As Integer
Dim As Integer i, count = 1
For i = 1 To n/2
If (n Mod i) = 0 Then count += 1
Next i
count_divisors = count
End Function
Print "Los primeros 20 anti-primos son:"
For j = 1 To 20
Do
divisors = count_divisors(Candidate)
If max_divisors < divisors Then
Results(j) = Candidate
max_divisors = divisors
Exit Do
End If
Candidate += 1
Loop
Next j
For j = 1 To 20
Print Results(j);
Next j
Print
Sleep
- Output:
Los primeros 20 anti-primos son: 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
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
- Output:
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
GW-BASIC
10 REM Anti-primes
20 DEFINT A-Z
30 N=1
40 IF S>=20 THEN END ELSE F=1
50 IF N<2 GOTO 80 ELSE FOR I=1 TO N\2
60 IF N MOD I=0 THEN F=F+1
70 NEXT
80 IF F<=M GOTO 120
90 PRINT N,
100 M=F
110 S=S+1
120 N=N+1
130 GOTO 40
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Another solution:
10 REM Anti-primes
20 C = -999
30 N = N + 1
40 GOSUB 70
50 IF T = 20 THEN END
60 GOTO 30
70 D = 0
80 FOR F = 1 TO INT(N/2)
90 IF N MOD F = 0 THEN D = D + 1
100 NEXT F
110 IF D > C THEN GOSUB 130
120 RETURN
130 C = D
140 T = T + 1
150 PRINT N
160 RETURN
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Palo Alto Tiny 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
- Output:
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
PureBasic
Procedure.i cntDiv(n.i)
Define.i i, count
If n < 2 : ProcedureReturn 1 : EndIf
count = 2 : i = 2
While i <= n / 2
If n % i = 0 : count + 1 : EndIf
i + 1
Wend
ProcedureReturn count
EndProcedure
; - - - MAIN - - -
Define.i n = 1, d, maxDiv = 0, count = 0
If OpenConsole("")
PrintN("The first 20 anti-primes are: ")
While count < 20
d = cntDiv(n)
If d > maxDiv
Print(Str(n) + " ")
maxDiv = d : count + 1
EndIf
n + 1
Wend
PrintN("")
Input()
EndIf
End 0
- Output:
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
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
- Output:
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
QuickBASIC
' Anti-primes
DECLARE FUNCTION DivisorCount (V%)
MaxAntiPrime% = 20
N% = 0: MaxDivisors% = 0: AntiPrimeCount% = 0
WHILE AntiPrimeCount% < MaxAntiPrime%
N% = N% + 1
Divisors% = DivisorCount(N%)
IF Divisors% > MaxDivisors% THEN
PRINT STR$(N%);
MaxDivisors% = Divisors%
AntiPrimeCount% = AntiPrimeCount% + 1
END IF
WEND
PRINT
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
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Run BASIC
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
- Output:
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
Tiny BASIC
100 REM Anti-primes
110 LET A=0
120 LET N=1
130 LET H=0
140 PRINT "The first 20 anti-primes are:"
150 GOSUB 300
160 LET H=F
170 LET A=A+1
180 PRINT N
190 LET N=N+1
200 IF A<20 THEN GOTO 150
210 END
290 REM Search next anti-prime
300 GOSUB 400
310 IF F>H THEN RETURN
320 LET N=N+1
330 GOTO 300
390 REM Count divisors
400 LET F=1
410 IF N>1 THEN LET F=2
420 LET C=2
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
- Output:
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
VBA
Private Function factors(n As Integer) As Collection
Dim f As New Collection
For i = 1 To Sqr(n)
If n Mod i = 0 Then
f.Add i
If n / i <> i Then f.Add n / i
End If
Next i
f.Add n
Set factors = f
End Function
Public Sub anti_primes()
Dim n As Integer, maxd As Integer
Dim res As New Collection, lenght As Integer
Dim lf As Integer
n = 1: maxd = -1
Length = 0
Do While res.count < 20
lf = factors(n).count
If lf > maxd Then
res.Add n
maxd = lf
End If
n = n + IIf(n > 1, 2, 1)
Loop
Debug.Print "The first 20 anti-primes are:";
For Each x In res
Debug.Print x;
Next x
End Sub
- Output:
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
Visual Basic .NET
Module Module1
Function CountDivisors(n As Integer) As Integer
If n < 2 Then
Return 1
End If
Dim count = 2 '1 and n
For i = 2 To n \ 2
If n Mod i = 0 Then
count += 1
End If
Next
Return count
End Function
Sub Main()
Dim maxDiv, count As Integer
Console.WriteLine("The first 20 anti-primes are:")
Dim n = 1
While count < 20
Dim d = CountDivisors(n)
If d > maxDiv Then
Console.Write("{0} ", n)
maxDiv = d
count += 1
End If
n += 1
End While
Console.WriteLine()
End Sub
End Module
- Output:
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
Yabasic
print "The first 20 anti-primes are:"
while (count < 20)
n = n + 1
d = count_divisors(n)
if d > max_divisors then
print n;
max_divisors = d
count = count + 1
end if
wend
print
sub count_divisors(n)
local count, i
if n < 2 return 1
count = 2
for i = 2 to n/2
if not(mod(n, i)) count = count + 1
next
return count
end sub
- Output:
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
// First 20 antiprimes.
sub count_factors(number)
local count, attempt
for attempt = 1 to number
if not mod(number, attempt) count = count + 1
next
return count
end sub
sub antiprimes$(goal)
local factors, list$, number, mostFactors, nitems
number = 1
while nitems < goal
factors = count_factors(number)
if factors > mostFactors then
list$ = list$ + ", " + str$(number)
nitems = nitems + 1
mostFactors = factors
endif
number = number + 1
wend
return list$
end sub
print "The first 20 antiprimes:"
print mid$(antiprimes$(20), 3)
print "Done."
- Output:
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.
BCPL
get "libhdr"
manifest $( LIMIT = 20 $)
let nfactors(n) =
n < 2 -> 1, valof
$( let c = 2
for i=2 to n/2
if n rem i = 0 then c := c + 1
resultis c
$)
let start() be
$( let max = 0 and seen = 0 and n = 1
while seen < LIMIT
$( let f = nfactors(n)
if f > max
$( writef("%N ",n)
max := f
seen := seen + 1
$)
n := n + 1
$)
wrch('*N')
$)
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
C
#include <stdio.h>
int countDivisors(int n) {
int i, count;
if (n < 2) return 1;
count = 2; // 1 and n
for (i = 2; i <= n/2; ++i) {
if (n%i == 0) ++count;
}
return count;
}
int main() {
int n, d, maxDiv = 0, count = 0;
printf("The first 20 anti-primes are:\n");
for (n = 1; count < 20; ++n) {
d = countDivisors(n);
if (d > maxDiv) {
printf("%d ", n);
maxDiv = d;
count++;
}
}
printf("\n");
return 0;
}
- Output:
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
C#
using System;
using System.Linq;
using System.Collections.Generic;
public static class Program
{
public static void Main() =>
Console.WriteLine(string.Join(" ", FindAntiPrimes().Take(20)));
static IEnumerable<int> FindAntiPrimes() {
int max = 0;
for (int i = 1; ; i++) {
int divisors = CountDivisors(i);
if (divisors > max) {
max = divisors;
yield return i;
}
}
int CountDivisors(int n) => Enumerable.Range(1, n / 2).Count(i => n % i == 0) + 1;
}
}
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
C++
#include <iostream>
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;
}
int main() {
int maxDiv = 0, count = 0;
std::cout << "The first 20 anti-primes are:" << std::endl;
for (int n = 1; count < 20; ++n) {
int d = countDivisors(n);
if (d > maxDiv) {
std::cout << n << " ";
maxDiv = d;
count++;
}
}
std::cout << std::endl;
return 0;
}
- Output:
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
CLU
% Count factors
factors = proc (n: int) returns (int)
if n<2 then return(1) end
count: int := 2
for i: int in int$from_to(2, n/2) do
if n//i = 0 then count := count + 1 end
end
return(count)
end factors
% Generate antiprimes
antiprimes = iter () yields (int)
max: int := 0
n: int := 1
while true do
f: int := factors(n)
if f > max then
yield(n)
max := f
end
n := n + 1
end
end antiprimes
% Show the first 20 antiprimes
start_up = proc ()
max = 20
po: stream := stream$primary_output()
count: int := 0
for i: int in antiprimes() do
stream$puts(po, int$unparse(i) || " ")
count := count + 1
if count = max then break end
end
end start_up
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
COBOL
******************************************************************
* COBOL solution to Anti-primes challange
* The program was run on OpenCobolIDE
******************************************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. ANGLE-PRIMES.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 ANTI-PRIMES-CTR PIC 9(3) VALUE 0.
77 FACTORS-CTR PIC 9(3) VALUE 0.
77 WS-INTEGER PIC 9(5) VALUE 1.
77 WS-MAX PIC 9(5) VALUE 0.
77 WS-I PIc 9(5) VALUE 0.
77 WS-LIMIT PIC 9(5) VALUE 1.
77 WS-REMAINDER PIC 9(5).
01 OUT-HDR PIC X(23) VALUE 'SEQ ANTI-PRIME FACTORS'.
01 OUT-LINE.
05 OUT-SEQ PIC 9(3).
05 FILLER PIC X(3) VALUE SPACES.
05 OUT-ANTI PIC ZZZZ9.
05 FILLER PIC X(4) VALUE SPACES.
05 OUT-FACTORS PIC ZZZZ9.
PROCEDURE DIVISION.
000-MAIN.
DISPLAY OUT-HDR.
PERFORM 100-GET-ANTI-PRIMES
VARYING WS-INTEGER FROM 1 By 1
UNTIL ANTI-PRIMES-CTR >= 20.
STOP RUN.
100-GET-ANTI-PRIMES.
SET FACTORS-CTR TO 0.
COMPUTE WS-LIMIT = 1 + WS-INTEGER ** .5.
PERFORM 200-COUNT-FACTORS
VARYING WS-I FROM 1 BY 1
UNTIL WS-I >= WS-LIMIT.
IF FACTORS-CTR > WS-MAX
ADD 1 TO ANTI-PRIMES-CTR
COMPUTE WS-MAX = FACTORS-CTR
MOVE ANTI-PRIMES-CTR TO OUT-SEQ
MOVE WS-INTEGER TO OUT-ANTI
MOVE FACTORS-CTR TO OUT-FACTORS
DISPLAY OUT-LINE
END-IF.
200-COUNT-FACTORS.
COMPUTE WS-REMAINDER =
FUNCTION MOD(WS-INTEGER WS-I).
IF WS-REMAINDER = ZERO
ADD 1 TO FACTORS-CTR
IF WS-INTEGER NOT = WS-I ** 2
ADD 1 TO FACTORS-CTR
END-IF
END-IF.
******************************************************************
* OUTPUT:
******************************************************************
* SEQ ANTI-PRIME FACTORS
* 001 1 1
* 002 2 2
* 003 4 3
* 004 6 4
* 005 12 6
* 006 24 8
* 007 36 9
* 008 48 10
* 009 60 12
* 010 120 16
* 011 180 18
* 012 240 20
* 013 360 24
* 014 720 30
* 015 840 32
* 016 1260 36
* 017 1680 40
* 018 2520 48
* 019 5040 60
* 020 7560 64
******************************************************************
Common Lisp
(defun factors (n &aux (lows '()) (highs '()))
(do ((limit (1+ (isqrt n))) (factor 1 (1+ factor)))
((= factor limit)
(when (= n (* limit limit))
(push limit highs))
(remove-duplicates (nreconc lows highs)))
(multiple-value-bind (quotient remainder) (floor n factor)
(when (zerop remainder)
(push factor lows)
(push quotient highs)))))
(defun anti-prime ()
(format t "The first 20 anti-primes are :~%")
(do ((dmax 0) (c 0) (i 0 (1+ i)))
((= c 20))
(setf facts (list-length (factors i)))
(when (< dmax facts)
(format t "~d " i)
(setq dmax facts)
(incf c))))
- Output:
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
Cowgol
include "cowgol.coh";
const AMOUNT := 20;
sub countFactors(n: uint16): (count: uint16) is
var i: uint16 := 1;
count := 1;
while i <= n/2 loop
if n%i == 0 then
count := count + 1;
end if;
i := i + 1;
end loop;
end sub;
var max: uint16 := 0;
var seen: uint8 := 0;
var n: uint16 := 1;
var f: uint16 := 0;
while seen < AMOUNT loop;
f := countFactors(n);
if f > max then
print_i16(n);
print_char(' ');
max := f;
seen := seen + 1;
end if;
n := n + 1;
end loop;
print_nl();
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Crystal
def count_divisors(n : Int64) : Int64
return 1_i64 if n < 2
count = 2_i64
i = 2
while i <= n // 2
count += 1 if n % i == 0
i += 1
end
count
end
max_div = 0_i64
count = 0_i64
print "The first 20 anti-primes are: "
n = 1_i64
while count < 20
d = count_divisors n
if d > max_div
print "#{n} "
max_div = d
count += 1
end
n += 1
end
puts ""
- Output:
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
D
import std.stdio;
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, count;
writeln("The first 20 anti-primes are:");
for (int n = 1; count < 20; ++n) {
int d = countDivisors(n);
if (d > maxDiv) {
write(n, ' ');
maxDiv = d;
count++;
}
}
writeln;
}
- Output:
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
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("");
}
Delphi
See #Pascal.
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
.
.
Elixir
defmodule AntiPrimes do
def divcount(n) when is_integer(n), do: divcount(n, 1, 0)
def divcount(n, d, count) when d * d > n, do: count
def divcount(n, d, count) do
divs = case rem(n, d) do
0 ->
case n - d * d do
0 -> 1
_ -> 2
end
_ -> 0
end
divcount(n, d + 1, count + divs)
end
def antiprimes(n), do: antiprimes(n, 1, 0, [])
def antiprimes(0, _, _, l), do: Enum.reverse(l)
def antiprimes(n, m, max, l) do
count = divcount(m)
case count > max do
true -> antiprimes(n-1, m+1, count, [m|l])
false -> antiprimes(n, m+1, max, l)
end
end
def main() do
:io.format("The first 20 anti-primes are ~w~n", [antiprimes(20)])
end
end
- Output:
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]
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
write(n + " ")
maxDiv ← d
++count
end
writeLine()
- Output:
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
Erlang
divcount(N) -> divcount(N, 1, 0).
divcount(N, D, Count) when D*D > N -> Count;
divcount(N, D, Count) ->
Divs = case N rem D of
0 ->
case N - D*D of
0 -> 1;
_ -> 2
end;
_ -> 0
end,
divcount(N, D + 1, Count + Divs).
antiprimes(N) -> antiprimes(N, 1, 0, []).
antiprimes(0, _, _, L) -> lists:reverse(L);
antiprimes(N, M, Max, L) ->
Count = divcount(M),
case Count > Max of
true -> antiprimes(N-1, M+1, Count, [M|L]);
false -> antiprimes(N, M+1, Max, L)
end.
main(_) ->
io:format("The first 20 anti-primes are ~w~n", [antiprimes(20)]).
- Output:
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]
F#
The Function
This task uses Extensible Prime Generator (F#)
// Find Antı-Primes. Nigel Galloway: Secember 10th., 2018
let N=200000000000000000000000000I
let fI,_=Seq.scan(fun (_,g) e->(e,e*g)) (2I,4I) (primes|>Seq.skip 1|>Seq.map bigint)|>Seq.takeWhile(fun(_,n)->n<N)|>List.ofSeq|>List.unzip
let fG g=Seq.unfold(fun ((n,i,e) as z)->Some(z,(n+1,i+1,(e*g)))) (1,2,g)|>Seq.takeWhile(fun(_,_,n)->n<N)
let fE n i=n|>Seq.collect(fun(n,e,g)->Seq.map(fun(a,c,b)->(a,c*e,g*b)) (i|>Seq.takeWhile(fun(g,_,_)->g<=n)) |> Seq.takeWhile(fun(_,_,n)->n<N))
let fL,_=Seq.concat(Seq.scan(fun n g->fE n (fG g)) (seq[(2147483647,1,1I)]) fI)|>List.ofSeq|>List.sortBy(fun(_,_,n)->n)|>List.fold(fun ((a,b) as z) (_,n,g)->if n>b then ((n,g)::a,n) else z) ([],0)
The Task
printfn "The first 20 anti-primes are :-"; for (_,g) in (List.rev fL)|>List.take 20 do printfn "%A" g
- Output:
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
Extra Credit
printfn "There are %d anti-primes less than %A:-" (List.length fL) N; for (n,g) in (List.rev fL) do printfn "%A has %d dividers" g n
- Output:
There are 245 anti-primes less than 200000000000000000000000000:- 1 has 1 dividers 2 has 2 dividers 4 has 3 dividers 6 has 4 dividers 12 has 6 dividers 24 has 8 dividers 36 has 9 dividers 48 has 10 dividers 60 has 12 dividers 120 has 16 dividers 180 has 18 dividers 240 has 20 dividers 360 has 24 dividers 720 has 30 dividers 840 has 32 dividers 1260 has 36 dividers 1680 has 40 dividers 2520 has 48 dividers 5040 has 60 dividers 7560 has 64 dividers 10080 has 72 dividers 15120 has 80 dividers 20160 has 84 dividers 25200 has 90 dividers 27720 has 96 dividers 45360 has 100 dividers 50400 has 108 dividers 55440 has 120 dividers 83160 has 128 dividers 110880 has 144 dividers 166320 has 160 dividers 221760 has 168 dividers 277200 has 180 dividers 332640 has 192 dividers 498960 has 200 dividers 554400 has 216 dividers 665280 has 224 dividers 720720 has 240 dividers 1081080 has 256 dividers 1441440 has 288 dividers 2162160 has 320 dividers 2882880 has 336 dividers 3603600 has 360 dividers 4324320 has 384 dividers 6486480 has 400 dividers 7207200 has 432 dividers 8648640 has 448 dividers 10810800 has 480 dividers 14414400 has 504 dividers 17297280 has 512 dividers 21621600 has 576 dividers 32432400 has 600 dividers 36756720 has 640 dividers 43243200 has 672 dividers 61261200 has 720 dividers 73513440 has 768 dividers 110270160 has 800 dividers 122522400 has 864 dividers 147026880 has 896 dividers 183783600 has 960 dividers 245044800 has 1008 dividers 294053760 has 1024 dividers 367567200 has 1152 dividers 551350800 has 1200 dividers 698377680 has 1280 dividers 735134400 has 1344 dividers 1102701600 has 1440 dividers 1396755360 has 1536 dividers 2095133040 has 1600 dividers 2205403200 has 1680 dividers 2327925600 has 1728 dividers 2793510720 has 1792 dividers 3491888400 has 1920 dividers 4655851200 has 2016 dividers 5587021440 has 2048 dividers 6983776800 has 2304 dividers 10475665200 has 2400 dividers 13967553600 has 2688 dividers 20951330400 has 2880 dividers 27935107200 has 3072 dividers 41902660800 has 3360 dividers 48886437600 has 3456 dividers 64250746560 has 3584 dividers 73329656400 has 3600 dividers 80313433200 has 3840 dividers 97772875200 has 4032 dividers 128501493120 has 4096 dividers 146659312800 has 4320 dividers 160626866400 has 4608 dividers 240940299600 has 4800 dividers 293318625600 has 5040 dividers 321253732800 has 5376 dividers 481880599200 has 5760 dividers 642507465600 has 6144 dividers 963761198400 has 6720 dividers 1124388064800 has 6912 dividers 1606268664000 has 7168 dividers 1686582097200 has 7200 dividers 1927522396800 has 7680 dividers 2248776129600 has 8064 dividers 3212537328000 has 8192 dividers 3373164194400 has 8640 dividers 4497552259200 has 9216 dividers 6746328388800 has 10080 dividers 8995104518400 has 10368 dividers 9316358251200 has 10752 dividers 13492656777600 has 11520 dividers 18632716502400 has 12288 dividers 26985313555200 has 12960 dividers 27949074753600 has 13440 dividers 32607253879200 has 13824 dividers 46581791256000 has 14336 dividers 48910880818800 has 14400 dividers 55898149507200 has 15360 dividers 65214507758400 has 16128 dividers 93163582512000 has 16384 dividers 97821761637600 has 17280 dividers 130429015516800 has 18432 dividers 195643523275200 has 20160 dividers 260858031033600 has 20736 dividers 288807105787200 has 21504 dividers 391287046550400 has 23040 dividers 577614211574400 has 24576 dividers 782574093100800 has 25920 dividers 866421317361600 has 26880 dividers 1010824870255200 has 27648 dividers 1444035528936000 has 28672 dividers 1516237305382800 has 28800 dividers 1732842634723200 has 30720 dividers 2021649740510400 has 32256 dividers 2888071057872000 has 32768 dividers 3032474610765600 has 34560 dividers 4043299481020800 has 36864 dividers 6064949221531200 has 40320 dividers 8086598962041600 has 41472 dividers 10108248702552000 has 43008 dividers 12129898443062400 has 46080 dividers 18194847664593600 has 48384 dividers 20216497405104000 has 49152 dividers 24259796886124800 has 51840 dividers 30324746107656000 has 53760 dividers 36389695329187200 has 55296 dividers 48519593772249600 has 57600 dividers 60649492215312000 has 61440 dividers 72779390658374400 has 62208 dividers 74801040398884800 has 64512 dividers 106858629141264000 has 65536 dividers 112201560598327200 has 69120 dividers 149602080797769600 has 73728 dividers 224403121196654400 has 80640 dividers 299204161595539200 has 82944 dividers 374005201994424000 has 86016 dividers 448806242393308800 has 92160 dividers 673209363589963200 has 96768 dividers 748010403988848000 has 98304 dividers 897612484786617600 has 103680 dividers 1122015605983272000 has 107520 dividers 1346418727179926400 has 110592 dividers 1795224969573235200 has 115200 dividers 2244031211966544000 has 122880 dividers 2692837454359852800 has 124416 dividers 3066842656354276800 has 129024 dividers 4381203794791824000 has 131072 dividers 4488062423933088000 has 138240 dividers 6133685312708553600 has 147456 dividers 8976124847866176000 has 153600 dividers 9200527969062830400 has 161280 dividers 12267370625417107200 has 165888 dividers 15334213281771384000 has 172032 dividers 18401055938125660800 has 184320 dividers 27601583907188491200 has 193536 dividers 30668426563542768000 has 196608 dividers 36802111876251321600 has 207360 dividers 46002639845314152000 has 215040 dividers 55203167814376982400 has 221184 dividers 73604223752502643200 has 230400 dividers 92005279690628304000 has 245760 dividers 110406335628753964800 has 248832 dividers 131874234223233902400 has 258048 dividers 184010559381256608000 has 276480 dividers 263748468446467804800 has 294912 dividers 368021118762513216000 has 307200 dividers 395622702669701707200 has 322560 dividers 527496936892935609600 has 331776 dividers 659371171116169512000 has 344064 dividers 791245405339403414400 has 368640 dividers 1186868108009105121600 has 387072 dividers 1318742342232339024000 has 393216 dividers 1582490810678806828800 has 414720 dividers 1978113513348508536000 has 430080 dividers 2373736216018210243200 has 442368 dividers 3164981621357613657600 has 460800 dividers 3956227026697017072000 has 491520 dividers 4747472432036420486400 has 497664 dividers 5934340540045525608000 has 516096 dividers 7912454053394034144000 has 552960 dividers 11868681080091051216000 has 589824 dividers 15824908106788068288000 has 614400 dividers 17407398917466875116800 has 622080 dividers 18594267025475980238400 has 645120 dividers 23737362160182102432000 has 663552 dividers 30990445042459967064000 has 688128 dividers 34814797834933750233600 has 691200 dividers 37188534050951960476800 has 737280 dividers 52222196752400625350400 has 746496 dividers 55782801076427940715200 has 774144 dividers 61980890084919934128000 has 786432 dividers 74377068101903920953600 has 829440 dividers 92971335127379901192000 has 860160 dividers 111565602152855881430400 has 884736 dividers 148754136203807841907200 has 921600 dividers 185942670254759802384000 has 983040 dividers 223131204305711762860800 has 995328 dividers 278914005382139703576000 has 1032192 dividers 371885340509519604768000 has 1105920 dividers 557828010764279407152000 has 1179648 dividers 743770681019039209536000 has 1228800 dividers 818147749120943130489600 has 1244160 dividers 985496152350226952635200 has 1290240 dividers 1115656021528558814304000 has 1327104 dividers 1487541362038078419072000 has 1351680 dividers 1636295498241886260979200 has 1382400 dividers 1970992304700453905270400 has 1474560 dividers 2454443247362829391468800 has 1492992 dividers 2956488457050680857905600 has 1548288 dividers 3284987174500756508784000 has 1572864 dividers 3941984609400907810540800 has 1658880 dividers 4927480761751134763176000 has 1720320 dividers 5912976914101361715811200 has 1769472 dividers 7883969218801815621081600 has 1843200 dividers 9854961523502269526352000 has 1966080 dividers 11825953828202723431622400 has 1990656 dividers 14782442285253404289528000 has 2064384 dividers 19709923047004539052704000 has 2211840 dividers 29564884570506808579056000 has 2359296 dividers 39419846094009078105408000 has 2457600 dividers 43361830703409985915948800 has 2488320 dividers 54202288379262482394936000 has 2580480 dividers 59129769141013617158112000 has 2654208 dividers 78839692188018156210816000 has 2703360 dividers 86723661406819971831897600 has 2764800 dividers 108404576758524964789872000 has 2949120 dividers 130085492110229957747846400 has 2985984 dividers 162606865137787447184808000 has 3096576 dividers 193814243295544634018256000 has 3145728 dividers
Factor
USING: assocs formatting kernel locals make math
math.primes.factors sequences.extras ;
IN: rosetta-code.anti-primes
<PRIVATE
: count-divisors ( n -- m )
dup 1 = [ group-factors values [ 1 + ] map-product ] unless ;
: (n-anti-primes) ( md n count -- ?md' n' ?count' )
dup 0 >
[| max-div! n count! |
n count-divisors :> d
d max-div > [ d max-div! n , count 1 - count! ] when
max-div n dup 60 >= 20 1 ? + count (n-anti-primes)
] when ;
PRIVATE>
: n-anti-primes ( n -- seq )
[ 0 1 ] dip [ (n-anti-primes) 3drop ] { } make ;
: anti-primes-demo ( -- )
20 n-anti-primes "First 20 anti-primes:\n%[%d, %]\n" printf ;
MAIN: anti-primes-demo
- Output:
First 20 anti-primes: { 1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560 }
Forth
This task uses Factors of an Integer with vectored execution
include ./factors.fs
: max-count ( n1 n2 -- n f )
\ n is max(n1, factor-count n2); if n is new maximum then f = true.
\
count-factors 2dup <
if nip true
else drop false
then ;
: .anti-primes ( n -- )
0 1 rot \ stack: max, candidate, count
begin
>r dup >r max-count
if r> dup . r> 1-
else r> r>
then swap 1+ swap
dup 0= until drop 2drop ;
." The first 20 anti-primes are: " 20 .anti-primes cr
bye
- Output:
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
Fortran
program anti_primes
use iso_fortran_env, only: output_unit
implicit none
integer :: n, d, maxDiv, pCount
write(output_unit,*) "The first 20 anti-primes are:"
n = 1
maxDiv = 0
pCount = 0
do
if (pCount >= 20) exit
d = countDivisors(n)
if (d > maxDiv) then
write(output_unit,'(I0,x)', advance="no") n
maxDiv = d
pCount = pCount + 1
end if
n = n + 1
end do
write(output_unit,*)
contains
pure function countDivisors(n)
integer, intent(in) :: n
integer :: countDivisors
integer :: i
countDivisors = 1
if (n < 2) return
countDivisors = 2
do i = 2, n/2
if (modulo(n, i) == 0) countDivisors = countDivisors + 1
end do
end function countDivisors
end program anti_primes
- Output:
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
Frink
smallest = 0
n = 1
results = new array
do
{
len = length[allFactors[n]]
if len > smallest
{
results.push[n]
smallest = len
}
n = n + 1
} until length[results] == 20
println[join[" ", results]]
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
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
- Output:
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
Go
Simple brute force approach which is quick enough here.
package main
import "fmt"
func countDivisors(n int) int {
if n < 2 {
return 1
}
count := 2 // 1 and n
for i := 2; i <= n/2; i++ {
if n%i == 0 {
count++
}
}
return count
}
func main() {
fmt.Println("The first 20 anti-primes are:")
maxDiv := 0
count := 0
for n := 1; count < 20; n++ {
d := countDivisors(n)
if d > maxDiv {
fmt.Printf("%d ", n)
maxDiv = d
count++
}
}
fmt.Println()
}
- Output:
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
Grain
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
}
}
- Output:
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
Groovy
Solution (uses Factors of an integer function "factorize()"):
def getAntiPrimes(def limit = 10) {
def antiPrimes = []
def candidate = 1L
def maxFactors = 0
while (antiPrimes.size() < limit) {
def factors = factorize(candidate)
if (factors.size() > maxFactors) {
maxFactors = factors.size()
antiPrimes << candidate
}
candidate++
}
antiPrimes
}
Test:
println (getAntiPrimes(20))
Output:
[1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560]
Haskell
import Data.List (find, group)
import Data.Maybe (fromJust)
firstPrimeFactor :: Int -> Int
firstPrimeFactor n = head $ filter ((0 ==) . mod n) [2 .. n]
allPrimeFactors :: Int -> [Int]
allPrimeFactors 1 = []
allPrimeFactors n =
let first = firstPrimeFactor n
in first : allPrimeFactors (n `div` first)
factorCount :: Int -> Int
factorCount 1 = 1
factorCount n = product ((succ . length) <$> group (allPrimeFactors n))
divisorCount :: Int -> (Int, Int)
divisorCount = (,) <*> factorCount
hcnNext :: (Int, Int) -> (Int, Int)
hcnNext (num, factors) =
fromJust $ find ((> factors) . snd) (divisorCount <$> [num ..])
hcnSequence :: [Int]
hcnSequence = fst <$> iterate hcnNext (1, 1)
main :: IO ()
main = print $ take 20 hcnSequence
- Output:
[1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560]
J
NB. factor count is the product of the incremented powers of prime factors
factor_count =: [: */ [: >: _&q:
NB. N are the integers 1 to 10000
NB. FC are the corresponding factor counts
FC =: factor_count&> N=: >: i. 10000
NB. take from the integers N{~
NB. the indexes of truth I.
NB. the vector which doesn't equal itself when rotated by one position (~: _1&|.)
NB. where that vector is the maximum over all prefixes of the factor counts >./\FC
N{~I.(~: _1&|.)>./\FC
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Java
public class Antiprime {
static 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;
}
public static void main(String[] args) {
int maxDiv = 0, count = 0;
System.out.println("The first 20 anti-primes are:");
for (int n = 1; count < 20; ++n) {
int d = countDivisors(n);
if (d > maxDiv) {
System.out.printf("%d ", n);
maxDiv = d;
count++;
}
}
System.out.println();
}
}
- Output:
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
JavaScript
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;
}
function go() {
var number = document.getElementById("n").value;
document.body.removeChild(document.getElementById("result-list"));
document.body.appendChild(showList(generateAntiprimes(number)));
}
function showList(array) {
var list = document.createElement("ul");
list.id = "result-list";
for (var i = 0; i < array.length; i++) {
var item = document.createElement("li");
item.appendChild(document.createTextNode(array[i]));
list.appendChild(item);
}
return list;
}
Html to test with some styling
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <script src="antiprimes.js"></script> <title>Anti-Primes</title> <style> body {padding: 50px;width: 50%;box-shadow: 0 0 15px 0 rgba(0, 0, 0, 0.25);margin: 15px auto;font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;letter-spacing: 1px;} a {color: #00aadd;text-decoration: none;} input {width: 50px;text-align: center;} ul {list-style: none;padding: 0;margin: 0;width: 25%;margin: auto;border: 1px solid #aaa;} li {text-align: center;background-color: #eaeaea;} li:nth-child(even) {background: #fff;} </style> </head> <body onload="go()"> <h1>Anti-Primes</h1> <div class="info"> The <a href="https://youtu.be/2JM2oImb9Qg">anti-primes</a> (or <a href="https://en.wikipedia.org/wiki/Highly_composite_number">highly composite numbers</a>, sequence <a href="https://oeis.org/A002182">A002182</a> in the <a href="https://oeis.org/">OEIS</a>) are the natural numbers with more factors than any smaller than itself. </div> <p>Generate first <input id="n" type="text" placeholder="Enter the number" value="20" /> anti-primes. <button onclick="go()">Go</button></p> <ul id="result-list"></ul> </body> </html>
jq
Works with gojq, the Go implementation of jq
# Compute the number of divisors, without calling sqrt
def ndivisors:
def sum(s): reduce s as $x (null; .+$x);
if . == 1 then 1
else . as $n
| sum( label $out
| range(1; $n) as $i
| ($i * $i) as $i2
| if $i2 > $n then break $out
else if $i2 == $n
then 1
elif ($n % $i) == 0
then 2
else empty
end
end)
end;
# Emit the antiprimes as a stream
def antiprimes:
1,
foreach range(2; infinite; 2) as $i ({maxfactors: 1};
.emit = null
| ($i | ndivisors) as $nfactors
| if $nfactors > .maxfactors
then .emit = $i
| .maxfactors = $nfactors
else .
end;
select(.emit).emit);
"The first 20 anti-primes are:", limit(20; antiprimes)
- Output:
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
Julia
using Primes, Combinatorics
function antiprimes(N, maxn = 2000000)
antip = [1] # special case: 1 is antiprime
count = 1
maxfactors = 1
for i in 2:2:maxn # antiprimes > 2 should be even
lenfac = length(unique(sort(collect(combinations(factor(Vector, i)))))) + 1
if lenfac > maxfactors
push!(antip, i)
if length(antip) >= N
return antip
end
maxfactors = lenfac
end
end
antip
end
println("The first 20 anti-primes are:\n", antiprimes(20))
println("The first 40 anti-primes are:\n", antiprimes(40))
- Output:
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] The first 40 anti-primes are: [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, 45360, 50400, 55440, 83160, 110880, 166320, 221760, 277200, 332640, 498960, 554400, 665280, 720720, 1081080, 1441440]
Kotlin
// Version 1.3.10
fun countDivisors(n: Int): Int {
if (n < 2) return 1;
var count = 2 // 1 and n
for (i in 2..n / 2) {
if (n % i == 0) count++
}
return count;
}
fun main(args: Array<String>) {
println("The first 20 anti-primes are:")
var maxDiv = 0
var count = 0
var n = 1
while (count < 20) {
val d = countDivisors(n)
if (d > maxDiv) {
print("$n ")
maxDiv = d
count++
}
n++
}
println()
}
- Output:
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
Lambdatalk
1) lambdatalk only
{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
2) using javascript
Lambdatalk can call javascript code, here simply copying the code written in the javascript entry.
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
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, cnt = 0, 0
for n = 1; cnt < 20; n += 1 {
val d = countDivisors(n)
if d > maxDiv {
write n, " "
maxDiv = d
cnt += 1
}
}
writeln()
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Lua
Counting the factors using modulo
-- First 20 antiprimes.
function count_factors(number)
local count = 0
for attempt = 1, number do
local remainder = number % attempt
if remainder == 0 then
count = count + 1
end
end
return count
end
function antiprimes(goal)
local list, number, mostFactors = {}, 1, 0
while #list < goal do
local factors = count_factors(number)
if factors > mostFactors then
table.insert(list, number)
mostFactors = factors
end
number = number + 1
end
return list
end
function recite(list)
for index, item in ipairs(list) do
print(item)
end
end
print("The first 20 antiprimes:")
recite(antiprimes(20))
print("Done.")
- Output:
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.
Using a table of divisor counts
-- 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 ), " " ) )
.
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Maple
antiprimes := proc(n)
local ap, i, max_divisors, num_divisors;
max_divisors := 0;
ap := [];
for i from 1 while numelems(ap) < n do
num_divisors := numelems(NumberTheory:-Divisors(i));
if num_divisors > max_divisors then
ap := [op(ap), i];
max_divisors := num_divisors;
end if;
end do;
return ap;
end proc:
antiprimes(20);
- Output:
[1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560]
Mathematica / Wolfram Language
sigma = DivisorSigma[0, #] &;
currentmax = -\[Infinity];
res = {};
Do[
s = sigma[v];
If[s > currentmax,
AppendTo[res, v];
currentmax = s;
];
If[Length[res] >= 25, Break[]]
,
{v, \[Infinity]}
]
res
- Output:
{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}
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( " " )
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Modula-2
MODULE Antiprimes;
FROM InOut IMPORT WriteCard, WriteLn;
CONST Amount = 20;
VAR max, seen, n, f: CARDINAL;
PROCEDURE factors(n: CARDINAL): CARDINAL;
VAR facs, div: CARDINAL;
BEGIN
IF n<2 THEN RETURN 1; END;
facs := 2;
FOR div := 2 TO n DIV 2 DO
IF n MOD div = 0 THEN
INC(facs);
END;
END;
RETURN facs;
END factors;
BEGIN
max := 0;
seen := 0;
n := 1;
WHILE seen < Amount DO
f := factors(n);
IF f > max THEN
WriteCard(n,5);
max := f;
INC(seen);
IF seen MOD 10 = 0 THEN WriteLn(); END;
END;
INC(n);
END;
END Antiprimes.
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Modula-3
MODULE AntiPrimes EXPORTS Main;
IMPORT IO,Fmt;
CONST
Amount = 20;
VAR
Max,Seen,N,F:CARDINAL;
PROCEDURE Factors(N:CARDINAL):CARDINAL =
VAR
Facts:CARDINAL;
BEGIN
IF N < 2 THEN RETURN 1 END;
Facts := 2;
FOR Div := 2 TO N DIV 2 DO
IF N MOD Div = 0 THEN INC(Facts) END;
END;
RETURN Facts;
END Factors;
BEGIN
Max := 0;
Seen := 0;
N := 1;
WHILE Seen < Amount DO
F := Factors(N);
IF F > Max THEN
IO.Put(Fmt.F("%5s",Fmt.Int(N)));
Max := F;
INC(Seen);
IF Seen MOD 10 = 0 THEN IO.Put("\n") END;
END;
INC(N);
END;
END AntiPrimes.
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Nanoquery
def countDivisors(n)
if (n < 2)
return 1
end
count = 2
for i in range(2, int(n/2))
if (n % i) = 0
count += 1
end
end
return count
end
maxDiv = 0
count = 0
println "The first 20 anti-primes are:"
for (n = 1) (count < 20) (n += 1)
d = countDivisors(n)
if d > maxDiv
print format("%d ", n)
maxDiv = d
count += 1
end
end
println
- Output:
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
Nim
# First 20 antiprimes
proc countDivisors(n: int): int =
if n < 2:
return 1
var count = 2
for i in countup(2, (n / 2).toInt()):
if n %% i == 0:
count += 1
return count
proc antiPrimes(n: int) =
echo("The first ", n, " anti-primes:")
var maxDiv = 0
var count = 0
var i = 1
while count < n:
let d = countDivisors(i)
if d > maxDiv:
echo(i)
maxDiv = d
count += 1
i += 1
antiPrimes(20)
- Output:
The first 20 anti-primes: 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Oberon-2
MODULE AntiPrimes;
IMPORT Out;
CONST
Amount = 20;
VAR
Max,Seen,N,F:INTEGER;
PROCEDURE Factors(N:INTEGER):INTEGER;
VAR
Facts,Div:INTEGER;
BEGIN
IF N < 2 THEN RETURN 1 END;
Facts := 2;
FOR Div := 2 TO N DIV 2 DO
IF N MOD Div = 0 THEN INC(Facts) END;
END;
RETURN Facts;
END Factors;
BEGIN
Max := 0;
Seen := 0;
N := 1;
WHILE Seen < Amount DO
F := Factors(N);
IF F > Max THEN
Out.Int(N,5);
Max := F;
INC(Seen);
IF Seen MOD 10 = 0 THEN Out.Ln END;
END;
INC(N);
END;
END AntiPrimes.
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Objeck
class AntiPrimes {
function : Main(args : String[]) ~ Nil {
maxDiv := 0; count := 0;
"The first 20 anti-primes are:"->PrintLine();
for(n := 1; count < 20; ++n;) {
d := CountDivisors(n);
if(d > maxDiv) {
"{$n} "->Print();
maxDiv := d;
count++;
};
};
'\n'->Print();
}
function : native : CountDivisors(n : Int) ~ Int {
if (n < 2) { return 1; };
count := 2;
for(i := 2; i <= n/2; ++i;) {
if(n%i = 0) { ++count; };
};
return count;
}
}
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Odin
Anti-Primes Odin Build: dev-2023-07-nightly:3072479c
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
}
- Output:
First 20 anti-primes -------------------- 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Pascal
Easy factoring without primes.Decided to show count of factors.
program AntiPrimes;
{$IFdef FPC}
{$MOde Delphi}
{$IFEND}
function getFactorCnt(n:NativeUint):NativeUint;
var
divi,quot,pot,lmt : NativeUint;
begin
result := 1;
divi := 1;
lmt := trunc(sqrt(n));
while divi < n do
Begin
inc(divi);
pot := 0;
repeat
quot := n div divi;
if n <> quot*divi then
BREAK;
n := quot;
inc(pot);
until false;
result := result*(1+pot);
//IF n= prime leave now
if divi > lmt then
BREAK;
end;
end;
var
i,Count,FacCnt,lastCnt: NativeUint;
begin
count := 0;
lastCnt := 0;
i := 1;
repeat
FacCnt := getFactorCnt(i);
if lastCnt < FacCnt then
Begin
write(i,'(',FacCnt,'),');
lastCnt:= FacCnt;
inc(Count);
if count = 12 then
Writeln;
end;
inc(i);
until Count >= 20;
writeln;
end.
;Output:
1(1),2(2),4(3),6(4),12(6),24(8),36(9),48(10),60(12),120(16),180(18),240(20), 360(24),720(30),840(32),1260(36),1680(40),2520(48),5040(60),7560(64)
PARI/GP
countfactors(n)={
my(count(m)= prod(i=1,#factor(m)~,factor(m)[i,2]+1));
v=vector(n);
v[1]=1;
for(x=2,n,
v[x]=v[x-1]+1;
while(count(v[x-1])>=count(v[x]),v[x]++));
return(v)}
countfactors(20)
PascalABC.NET
function countdiv(n: integer): integer;
begin
result := Range(1, n.Sqrt.Trunc).Count(x -> n mod x = 0) * 2;
if n.Sqrt.Round.sqr = n then result -= 1;
end;
function AntiPrimes(): sequence of integer;
begin
var maxDiv := 0;
var i := 1;
while True do
begin
var d := countdiv(i);
if d > maxDiv then
begin
yield i;
maxDiv := d;
end;
i += 1;
end;
end;
begin
AntiPrimes.Take(20).Println;
println;
AntiPrimes.Take(40).Println;
end.
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 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 45360 50400 55440 83160 110880 166320 221760 277200 332640 498960 554400 665280 720720 1081080 1441440
Perl
use ntheory qw(divisors);
my @anti_primes;
for (my ($k, $m) = (1, 0) ; @anti_primes < 20 ; ++$k) {
my $sigma0 = divisors($k);
if ($sigma0 > $m) {
$m = $sigma0;
push @anti_primes, $k;
}
}
printf("%s\n", join(' ', @anti_primes));
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Phix
with javascript_semantics integer n=1, maxd = -1 sequence res = {} while length(res)<20 do integer lf = length(factors(n,1)) if lf>maxd then res &= n maxd = lf end if n += iff(n>1?2:1) end while printf(1,"The first 20 anti-primes are: %V\n",{res})
- Output:
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}
Phixmonti
0 var count
0 var n
0 var max_divisors
"The first 20 anti-primes are:" print nl
def count_divisors
dup 2 < if
drop
1
else
2
swap 1 over 2 / 2 tolist
for
over swap mod not if swap 1 + swap endif
endfor
drop
endif
enddef
true
while
count 20 < dup if
n 1 + var n
n count_divisors
dup max_divisors > if
n print " " print
var max_divisors
count 1 + var count
else
drop
endif
endif
endwhile
nl
msec print
Picat
count_divisors(1) = 1.
count_divisors(N) = Count, N >= 2 =>
Count = 2,
foreach (I in 2..N/2)
if (N mod I == 0) then
Count := Count + 1
end
end.
main =>
println("The first 20 anti-primes are:"),
MaxDiv = 0,
Count = 0,
N = 1,
while (Count < 20)
D := count_divisors(N),
if (D > MaxDiv) then
printf("%d ", N),
MaxDiv := D,
Count := Count + 1
end,
N := N + 1
end,
nl.
- Output:
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
PicoLisp
(de factors (N)
(let C 1
(when (>= N 2)
(inc 'C)
(for (I 2 (>= (/ N 2) I) (inc I))
(and (=0 (% N I)) (inc 'C)) ) )
C ) )
(de anti (X)
(let (M 0 I 0 N 0)
(make
(while (> X I)
(inc 'N)
(let R (factors N)
(when (> R M)
(link N)
(setq M R)
(inc 'I) ) ) ) ) ) )
(println (anti 20))
- Output:
(1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560)
PILOT
C :n=1
:max=0
:seen=0
*number
U :*count
T (c>max):#n
C (c>max):seen=seen+1
C (c>max):max=c
:n=n+1
J (seen<20):*number
E :
*count
C (n=1):c=1
E (n=1):
C :c=2
:i=2
*cnloop
E (i>n/2):
C (i*(n/i)=n):c=c+1
:i=i+1
J :*cnloop
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
PL/0
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.
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
PL/I
antiprimes: procedure options(main);
/* count the factors of a number */
countFactors: procedure(n) returns(fixed);
declare (n, i, count) fixed;
if n<2 then return(1);
count = 1;
do i=1 to n/2;
if mod(n,i) = 0 then count = count + 1;
end;
return(count);
end countFactors;
declare maxFactors fixed static init (0);
declare seen fixed static init (0);
declare n fixed;
declare factors fixed;
do n=1 repeat(n+1) while(seen < 20);
factors = countFactors(n);
if factors > maxFactors then do;
put edit(n) (F(5));
maxFactors = factors;
seen = seen + 1;
if mod(seen,15) = 0 then put skip;
end;
end;
end antiprimes;
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
PL/M
100H:
/* CP/M CALLS */
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
/* PRINT A NUMBER */
PRINT$NUMBER: PROCEDURE (N);
DECLARE S (7) BYTE INITIAL ('..... $');
DECLARE (N, P) ADDRESS, C BASED P BYTE;
P = .S(5);
DIGIT:
P = P - 1;
C = N MOD 10 + '0';
N = N / 10;
IF N > 0 THEN GO TO DIGIT;
CALL PRINT(P);
END PRINT$NUMBER;
/* COUNT THE FACTORS OF A NUMBER */
COUNT$FACTORS: PROCEDURE (N) ADDRESS;
DECLARE (N, I, COUNT) ADDRESS;
IF N<2 THEN RETURN 1;
COUNT = 1;
DO I=1 TO N/2;
IF N MOD I = 0 THEN COUNT = COUNT + 1;
END;
RETURN COUNT;
END COUNT$FACTORS;
DECLARE MAX$FACTORS ADDRESS INITIAL (0);
DECLARE SEEN BYTE INITIAL (0);
DECLARE N ADDRESS INITIAL (1);
DECLARE FACTORS ADDRESS;
DO WHILE SEEN < 20;
FACTORS = COUNT$FACTORS(N);
IF FACTORS > MAX$FACTORS THEN DO;
CALL PRINT$NUMBER(N);
MAX$FACTORS = FACTORS;
SEEN = SEEN + 1;
END;
N = N + 1;
END;
CALL EXIT;
EOF
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Processing
void setup() {
int most_factors = 0;
IntList anti_primes = new IntList();
int n = 1;
while (anti_primes.size() < 20) {
int counter = 1;
for (int i = 1; i <= n / 2; i++) {
if (n % i == 0) {
counter++;
}
}
if (counter > most_factors) {
anti_primes.append(n);
most_factors = counter;
}
n++;
}
for (int num : anti_primes) {
print(num + " ");
}
}
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Prolog
divcount(N, Count) :- divcount(N, 1, 0, Count).
divcount(N, D, C, C) :- D*D > N, !.
divcount(N, D, C, Count) :-
succ(D, D2),
divs(N, D, A), plus(A, C, C2),
divcount(N, D2, C2, Count).
divs(N, D, 0) :- N mod D =\= 0, !.
divs(N, D, 1) :- D*D =:= N, !.
divs(_, _, 2).
antiprimes(N, L) :- antiprimes(N, 1, 0, [], L).
antiprimes(0, _, _, L, R) :- reverse(L, R), !.
antiprimes(N, M, Max, L, R) :-
divcount(M, Count),
succ(M, M2),
(Count > Max
-> succ(N0, N), antiprimes(N0, M2, Count, [M|L], R)
; antiprimes(N, M2, Max, L, R)).
main :-
antiprimes(20, X),
write("The first twenty anti-primes are "), write(X), nl,
halt.
?- main.
- Output:
The first twenty anti-primes are [1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560]
Python
Uses the fast prime function from Factors of an integer#Python
from itertools import chain, count, cycle, islice, accumulate
def factors(n):
def prime_powers(n):
for c in accumulate(chain([2, 1, 2], cycle([2,4]))):
if c*c > n: break
if n%c: continue
d,p = (), c
while not n%c:
n,p,d = n//c, p*c, d+(p,)
yield d
if n > 1: yield n,
r = [1]
for e in prime_powers(n):
r += [a*b for a in r for b in e]
return r
def antiprimes():
mx = 0
yield 1
for c in count(2,2):
if c >= 58: break
ln = len(factors(c))
if ln > mx:
yield c
mx = ln
for c in count(60,30):
ln = len(factors(c))
if ln > mx:
yield c
mx = ln
if __name__ == '__main__':
print(*islice(antiprimes(), 40)))
- Output:
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 45360 50400 55440 83160 110880 166320 221760 277200 332640 498960 554400 665280 720720 1081080 1441440 old algorithm (without count(60,30) part) time to find first 40 antiprimes: around 14 seconds new algorithm (with count(60,30) part) time to find first 40 antiprimes: around 0.4 seconds
Quackery
factors
is defined at Factors of an integer.
0 temp put
[] 0
[ 1+ dup factors size
dup temp share > iff
[ temp replace
dup dip join ]
else drop
over size 20 = until ]
temp release
drop echo
- Output:
[ 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 ]
R
Uses brute force. My first entry!
# Antiprimes
max_divisors <- 0
findFactors <- function(x){
myseq <- seq(x)
myseq[(x %% myseq) == 0]
}
antiprimes <- vector()
x <- 1
n <- 1
while(length(antiprimes) < 20){
y <- findFactors(x)
if (length(y) > max_divisors){
antiprimes <- c(antiprimes, x)
max_divisors <- length(y)
n <- n + 1
}
x <- x + 1
}
antiprimes
- Output:
[1] 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Racket
#lang racket
(require racket/generator
math/number-theory)
(define (get-divisors n)
(apply * (map (λ (factor) (add1 (second factor))) (factorize n))))
(define antiprimes
(in-generator
(for/fold ([prev 0]) ([i (in-naturals 1)])
(define divisors (get-divisors i))
(when (> divisors prev) (yield i))
(max prev divisors))))
(for/list ([i (in-range 20)] [antiprime antiprimes]) antiprime)
- Output:
'(1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560)
Raku
(formerly Perl 6)
At its heart, this task is almost exactly the same as Proper_Divisors, it is just asking for slightly different results. Much of this code is lifted straight from there.
Implemented as an auto-extending lazy list. Displaying the count of anti-primes less than 5e5 also because... why not.
sub propdiv (\x) {
my @l = 1 if x > 1;
(2 .. x.sqrt.floor).map: -> \d {
unless x % d { @l.push: d; my \y = x div d; @l.push: y if y != d }
}
@l
}
my $last = 0;
my @anti-primes = lazy 1, |(|(2..59), 60, *+60 … *).grep: -> $c {
my \mx = +propdiv($c);
next if mx <= $last;
$last = mx;
$c
}
my $upto = 5e5;
put "First 20 anti-primes:\n{ @anti-primes[^20] }";
put "\nCount of anti-primes <= $upto: {+@anti-primes[^(@anti-primes.first: * > $upto, :k)]}";
- Output:
First 20 anti-primes: 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 Count of anti-primes <= 500000: 35
Red
Red []
inc: func ['v] [set v 1 + get v] ;; shortcut function for n: n + 1
n: 0 found: 0 max_div: 0
print "the first 20 anti-primes are:"
while [ inc n] [
nDiv: 1 ;; count n / n extra
if n > 1 [ repeat div n / 2 [ if n % div = 0 [inc nDiv] ] ]
if nDiv > max_div [
max_div: nDiv
prin [n ""]
if 20 <= inc found [halt]
]
]
- Output:
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 (halted)
REXX
even and odd numbers
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 (also known as highly─composite numbers).
The #DIVS function could be further optimized by only processing even numbers, with unity being treated as a special case.
/*REXX program finds and displays N number of anti-primes (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 */
Say '-index- --anti-prime--' /* display a title For the numbers shown */
nn=0 /* the count of anti-primes found " " */
Do i=1 For 59 While nn<N /* step through possible numbers by twos */
d=nndivs(i) /* get nn divisors; */
If d>maxD Then Do /* found an anti-prime nn set new maxD */
maxD=d
nn=nn+1
Say center(nn,7) right(i,10) /* display the index and the anti-prime. */
End
End /*i*/
Do i=60 by 20 While nn<N /* step through possible numbers by 20. */
d=nndivs(i)
If d>maxD Then Do /* found an anti-prime nn set new maxD */
maxD=d
nn=nn+1
Say center(nn,7) right(i,10) /* display the index and the anti-prime. */
End
End /*i*/
Exit /* stick a fork in it, we're all done. */
/*-----------------------------------------------------------------------------------*/
nndivs: Procedure /* compute the number of proper divisors */
Parse Arg x
If x<2 Then
Return 1
odd=x//2
n=1 /* 1 is a proper divisor */
Do j=2+odd by 1+odd While j*j<x /* test all possible integers */
/* up To but excluding sqrt(x) */
If x//j==0 Then /* j is a divisor,so is x%j */
n=n+2
End
If j*j==x Then /* If x is a square */
n=n+1 /* sqrt(x) is a proper divisor */
n=n+1 /* x is a proper divisor */
Return n
- output when using the default input of: 20
─index─ ──anti─prime── 1 1 2 2 3 4 4 6 5 12 6 24 7 36 8 48 9 60 10 120 11 180 12 240 13 360 14 720 15 840 16 1260 17 1680 18 2520 19 5040 20 7560
Took 0.015 seconds (ooRexx)
This version is perfectly adequate for the task.
- output when using the default input of: 55
─index─ ──anti─prime── 1 1 2 2 3 4 4 6 5 12 6 24 7 36 8 48 9 60 10 120 11 180 12 240 13 360 14 720 15 840 16 1260 17 1680 18 2520 19 5040 20 7560 21 10080 22 15120 23 20160 24 25200 25 27720 26 45360 27 50400 28 55440 29 83160 30 110880 31 166320 32 221760 33 277200 34 332640 35 498960 36 554400 37 665280 38 720720 39 1081080 40 1441440 41 2162160 42 2882880 43 3603600 44 4324320 45 6486480 46 7207200 47 8648640 48 10810800 49 14414400 50 17297280 51 21621600 52 32432400 53 36756720 54 43243200 55 61261200
Took > 1000 seconds (ooRexx).
only even numbers
This REXX version only processes even numbers (unity is treated as a special case.)
It's about 17% faster than the 1st REXX version.
/*REXX program finds and displays 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.*/
@.= .; @.1= 1; @.2= 2; @.4= 3; @.5= 2; @.6= 4
say '─index─ ──anti─prime──' /*display a title for the numbers shown*/
#= 1 /*the count of anti─primes found " " */
maxD= 1 /*the maximum number of divisors so far*/
say center(#, 7) right(1, 10) /*display the index and the anti─prime.*/
do once=1 for 1
do i=2 by 2 to 59 /*step through possible numbers by twos*/
d= #divs(i); if d<=maxD then iterate /*get # divisors; Is too small? Skip.*/
#= # + 1; maxD= d /*found an anti─prime #; set new minD.*/
say center(#, 7) right(i, 10) /*display the index and the anti─prime.*/
if #>=N then leave once /*if we have enough anti─primes, done. */
end /*i*/
do j=60 by 20 /*step through possible numbers by 20. */
d= #divs(j); if d<=maxD then iterate /*get # divisors; Is too small? Skip.*/
#= # + 1; maxD= d /*found an anti─prime #; set new minD.*/
say center(#, 7) right(j, 10) /*display the index and the anti─prime.*/
if #>=N then leave once /*if we have enough anti─primes, done. */
L= length(j) /*obtain the length of the index (J). */
if L>3 then j= j + left(4, L-2, 0) - 20 /*Length>3? Then calculate a long jump*/
end /*j*/
end /*once*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
#divs: parse arg x; if @.x\==. then return @.x /*if pre─computed, then return shortcut*/
$= 3; y= x % 2
/* [↑] start with known num of Pdivs.*/
do k=3 for x%2-3 while k<y
if x//k==0 then do; $= $ + 2 /*if no remainder, then found a divisor*/
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".*/
Running ooRexx, the first 20 antiprimes took 0.031 seconds, the first 55 still took > 1000 seconds.
- output is identical to the 1st REXX version.
Using REXX libraries and a little cheat
Libraries: How to use
Library: Functions
Library: Numbers
Library: Settings
Library: Abend
Both above versions use some 'knowledge' about the sequence yet to generate. I.e. the 2 loops with step size 2 and 20 and the calculation of a 'long jump'.
Now, let's stretch the idea of sequence knowledge. Inspecting the table given in the Wikipedia article, it is seen from common factors that then the further you come in the sequence, the higher increment you may take. See the second example for a 'calculated attempt'. Employing the idea fully, we arrive at following program.
include Settings
say version; say 'Anti-prims'; say
arg n
numeric digits 16
if n = '' then
n = 10000
show = (n > 0); n = Abs(n)
h = Highcomposites(n)
say h 'anti-primes found below' n
if show then do
do i = 1 to high.0
say i high.highcomposite.i
end
end
say time('e') 'seconds'
exit
Highcomposites:
/* Highly composite sequence */
procedure expose high.
arg x
/* Thresholds and increments */
a = '1 2 6 60 840 55440 720720 61261200 2327925600 321253732800 9999999999999'
b = '1 2 6 60 420 27720 360360 12252240 232792560 80313433200 9999999999999'
c = Words(a)-1; m = 0; n = 0
/* Colllect cf definition */
do i = 1 to c
do j = Word(a,i) by Word(b,i) to Min(x,Word(a,i+1)-1)
d = Divisors(j)
if d > m then do
n = n+1; high.highcomposite.n = j
m = d
end
end
end
high.0 = n
/* Return count */
return n
include Numbers
include Functions
include Abend
Running under ooRexx for 20 and 55 anti-primes
- Output:
REXX-ooRexx_5.0.0(MT)_64-bit 6.05 23 Dec 2022 Anti-primes 20 anti-primes found below 10000 1 1 2 2 3 4 4 6 5 12 6 24 7 36 8 48 9 60 10 120 11 180 12 240 13 360 14 720 15 840 16 1260 17 1680 18 2520 19 5040 20 7560 0 seconds REXX-ooRexx_5.0.0(MT)_64-bit 6.05 23 Dec 2022 Anti-primes 55 anti-primes found below 70000000 1 1 2 2 3 4 4 6 5 12 6 24 7 36 8 48 9 60 10 120 11 180 12 240 13 360 14 720 15 840 16 1260 17 1680 18 2520 19 5040 20 7560 21 10080 22 15120 23 20160 24 25200 25 27720 26 45360 27 50400 28 55440 29 83160 30 110880 31 166320 32 221760 33 277200 34 332640 35 498960 36 554400 37 665280 38 720720 39 1081080 40 1441440 41 2162160 42 2882880 43 3603600 44 4324320 45 6486480 46 7207200 47 8648640 48 10810800 49 14414400 50 17297280 51 21621600 52 32432400 53 36756720 54 43243200 55 61261200 0.591000 seconds
Let's challenge some higher numbers, without giving all the anti-primes (ooRexx).
- Output:
REXX-ooRexx_5.0.0(MT)_64-bit 6.05 23 Dec 2022 Anti-primes 56 anti-primes found below 100000000 0.609000 seconds REXX-ooRexx_5.0.0(MT)_64-bit 6.05 23 Dec 2022 Anti-primes 66 anti-primes found below 1000000000 1.696000 seconds REXX-ooRexx_5.0.0(MT)_64-bit 6.05 23 Dec 2022 Anti-primes 76 anti-primes found below 10000000000 6.595000 seconds REXX-ooRexx_5.0.0(MT)_64-bit 6.05 23 Dec 2022 Anti-primes 86 anti-primes found below 100000000000 77.317000 seconds REXX-ooRexx_5.0.0(MT)_64-bit 6.05 23 Dec 2022 Anti-primes 95 anti-primes found below 1000000000000 474.448000 seconds
Ring
Counting the divisors using modulo
# Project : Anti-primes
see "working..." + nl
see "wait for done..." + nl + nl
see "the first 20 anti-primes are:" + nl + nl
maxDivisor = 0
num = 0
n = 0
result = list(20)
while num < 20
n = n + 1
div = factors(n)
if (div > maxDivisor)
maxDivisor = div
num = num + 1
result[num] = n
ok
end
see "["
for n = 1 to len(result)
if n < len(result)
see string(result[n]) + ","
else
see string(result[n]) + "]" + nl + nl
ok
next
see "done..." + nl
func factors(an)
ansum = 2
if an < 2
return(1)
ok
for nr = 2 to an/2
if an%nr = 0
ansum = ansum+1
ok
next
return ansum
- Output:
working... wait for done... 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] done...
Counting the divisors using a table
# 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
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
RPL
We use here complex numbers to limit the stack size and ease its handling.
≪ → nb ≪ 1 1 nb 2 / FOR j nb j MOD NOT + NEXT ≫ ≫ ‘NDIV’ STO ≪ 1 - → items ≪ { } (2,0) DO DUP RE NDIV IF OVER IM OVER < THEN SWAP RE SWAP R→C SWAP OVER RE + SWAP ELSE DROP END 1 + UNTIL OVER SIZE items > END DROP ≫ ≫ ‘ANTIP’ STO
15 ANTIP
- Output:
{ 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 }
Ruby
require 'prime'
def num_divisors(n)
n.prime_division.inject(1){|prod, (_p,n)| prod * (n + 1) }
end
anti_primes = Enumerator.new do |y| # y is the yielder
max = 0
y << 1 # yield 1
2.step(nil,2) do |candidate| # nil is taken as Infinity
num = num_divisors(candidate)
if num > max
y << candidate # yield the candidate
max = num
end
end
end
puts anti_primes.take(20).join(" ")
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Rust
fn count_divisors(n: u64) -> usize {
if n < 2 {
return 1;
}
2 + (2..=(n / 2)).filter(|i| n % i == 0).count()
}
fn main() {
println!("The first 20 anti-primes are:");
(1..)
.scan(0, |max, n| {
let d = count_divisors(n);
Some(if d > *max {
*max = d;
Some(n)
} else {
None
})
})
.flatten()
.take(20)
.for_each(|n| print!("{} ", n));
println!();
}
- Output:
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
Scala
This program uses an iterator to count the factors of a number, then builds a lazily evaluated list of all anti-primes. Finding the first 20 anti-primes involves merely taking the first 20 elements of the list.
def factorCount(num: Int): Int = Iterator.range(1, num/2 + 1).count(num%_ == 0) + 1
def antiPrimes: LazyList[Int] = LazyList.iterate((1: Int, 1: Int)){case (n, facs) => Iterator.from(n + 1).map(i => (i, factorCount(i))).dropWhile(_._2 <= facs).next}.map(_._1)
- Output:
scala> print(antiPrimes.take(20).mkString(", ")) 1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560
Seed7
$ include "seed7_05.s7i";
const func integer: countDivisors (in integer: number) is func
result
var integer: count is 1;
local
var integer: num is 0;
begin
for num range 1 to number div 2 do
if number rem num = 0 then
incr(count);
end if;
end for;
end func;
const proc: main is func
local
var integer: maxDiv is 0;
var integer: count is 0;
var integer: number is 1;
var integer: divisors is 1;
begin
writeln("The first 20 anti-primes are:");
while count < 20 do
divisors := countDivisors(number);
if divisors > maxDiv then
write(number <& " ");
maxDiv := divisors;
incr(count);
end if;
incr(number);
end while;
writeln;
end func;
- Output:
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
Sidef
Using the built-in Number.sigma0 method to count the number of divisors.
say with (0) {|max|
1..Inf -> lazy.grep { (.sigma0 > max) && (max = .sigma0) }.first(20)
}
- Output:
[1, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560]
Swift
extension BinaryInteger {
@inlinable
public func countDivisors() -> Int {
var workingN = self
var count = 1
while workingN & 1 == 0 {
workingN >>= 1
count += 1
}
var d = Self(3)
while d * d <= workingN {
var (quo, rem) = workingN.quotientAndRemainder(dividingBy: d)
if rem == 0 {
var dc = 0
while rem == 0 {
dc += count
workingN = quo
(quo, rem) = workingN.quotientAndRemainder(dividingBy: d)
}
count += dc
}
d += 2
}
return workingN != 1 ? count * 2 : count
}
}
var antiPrimes = [Int]()
var maxDivs = 0
for n in 1... {
guard antiPrimes.count < 20 else {
break
}
let divs = n.countDivisors()
if maxDivs < divs {
maxDivs = divs
antiPrimes.append(n)
}
}
print("First 20 anti-primes are \(Array(antiPrimes))")
- Output:
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]
Tcl
proc countDivisors {n} {
if {$n < 2} {return 1}
set count 2
set n2 [expr $n / 2]
for {set i 2} {$i <= $n2} {incr i} {
if {[expr $n % $i] == 0} {incr count}
}
return $count
}
# main
set maxDiv 0
set count 0
puts "The first 20 anti-primes are:"
for {set n 1} {$count < 20} {incr n} {
set d [countDivisors $n]
if {$d > $maxDiv} {
puts $n
set maxDiv $d
incr count
}
}
- Output:
./anti_primes.tclThe 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
Transd
#lang transd
MainModule: {
countDivs: (λ n Int() ret_ Int()
(= ret_ 2)
(for i in Range(2 (to-Int (/ (to-Double n) 2) 1)) do
(if (not (mod n i)) (+= ret_ 1)))
(ret ret_)
),
_start: (λ locals: max 0 tmp 0 N 1 i 2
(textout 1 " ")
(while (< N 20)
(= tmp (countDivs i))
(if (> tmp max)
(textout i " ") (= max tmp) (+= N 1))
(+= i 1)
))
}
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
Uiua
# slightly faster than the simplistic /+=0◿+1⇡.
NDivs ← ⟨+2/+=0◿(↘1+1⇡⌊÷2).|1⟩<2.
⇌⊙◌◌⍢(
# Inc n, get NDiv: if >m, join n to accum, store new m.
⟨◌|⟜(⊂⊢)⍜(⊡1|◌):⟩:⟜<NDivs°⊟.⍜⊢(+1)
|
⋅(<:⧻) # Repeat until we have enough values.
)0_0 [] 20 # [n m] [accum] limit
- Output:
[1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560]
Vala
int count_divisors(int n) {
if (n < 2) return 1;
var count = 2;
for (int i = 2; i <= n/2; ++i)
if (n%i == 0) ++count;
return count;
}
void main() {
var max_div = 0;
var count = 0;
stdout.printf("The first 20 anti-primes are:\n");
for (int n = 1; count < 20; ++n) {
var d = count_divisors(n);
if (d > max_div) {
stdout.printf("%d ", n);
max_div = d;
count++;
}
}
stdout.printf("\n");
}
- Output:
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
V (Vlang)
fn count_divisors(n int) int {
if n < 2 {
return 1
}
mut count := 2 // 1 and n
for i := 2; i <= n/2; i++ {
if n%i == 0 {
count++
}
}
return count
}
fn main() {
println("The first 20 anti-primes are:")
mut max_div := 0
mut count := 0
for n := 1; count < 20; n++ {
d := count_divisors(n)
if d > max_div {
print("$n ")
max_div = d
count++
}
}
println('')
}
- Output:
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
Wren
import "./math" for Int
System.print("The first 20 anti-primes are:")
var maxDiv = 0
var count = 0
var n = 1
while (count < 20) {
var d = Int.divisors(n).count
if (d > maxDiv) {
System.write("%(n) ")
maxDiv = d
count = count + 1
}
n = n + 1
}
System.print()
- Output:
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
XPL0
int Counter, Num, Cnt, Div, Max;
[Counter:= 0;
Max:= 0;
Num:= 1;
loop [Cnt:= 0;
Div:= 1;
repeat if rem(Num/Div) = 0 then Cnt:= Cnt+1;
Div:= Div+1;
until Div > Num;
if Cnt > Max then
[IntOut(0, Num); ChOut(0, ^ );
Max:= Cnt;
Counter:= Counter+1;
if Counter >= 20 then quit;
];
Num:= Num+1;
];
]
- Output:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
zkl
fcn properDivsN(n) //--> count of proper divisors. 1-->1, wrong but OK here
{ [1.. (n + 1)/2 + 1].reduce('wrap(p,i){ p + (n%i==0 and n!=i) }) }
fcn antiPrimes{ // -->iterator
Walker.chain([2..59],[60..*,30]).tweak(fcn(c,rlast){
last,mx := rlast.value, properDivsN(c);
if(mx<=last) return(Void.Skip);
rlast.set(mx);
c
}.fp1(Ref(0))).push(1); // 1 has no proper divisors
}
println("First 20 anti-primes:\n ",antiPrimes().walk(20).concat(" "));
- Output:
First 20 anti-primes: 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
- Prime Numbers
- Programming Tasks
- Solutions by Programming Task
- 11l
- 8086 Assembly
- AArch64 Assembly
- Action!
- Ada
- ALGOL 68
- ALGOL W
- AppleScript
- APL
- ARM Assembly
- Arturo
- AWK
- BASIC
- BASIC256
- FreeBASIC
- Gambas
- GW-BASIC
- Palo Alto Tiny BASIC
- PureBasic
- QBasic
- QuickBASIC
- Run BASIC
- Tiny BASIC
- VBA
- Visual Basic .NET
- Yabasic
- BCPL
- C
- C sharp
- C++
- CLU
- COBOL
- Common Lisp
- Cowgol
- Crystal
- D
- Dart
- Delphi
- EasyLang
- Elixir
- EMal
- Erlang
- F Sharp
- Factor
- Forth
- Fortran
- Frink
- FutureBasic
- Go
- Grain
- Groovy
- Haskell
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Lambdatalk
- Langur
- Lua
- Maple
- Mathematica
- Wolfram Language
- MiniScript
- Modula-2
- Modula-3
- Nanoquery
- Nim
- Oberon-2
- Objeck
- Odin
- Pascal
- PARI/GP
- PascalABC.NET
- Perl
- Ntheory
- Phix
- Phixmonti
- Picat
- PicoLisp
- PILOT
- PL/0
- PL/I
- PL/M
- Processing
- Prolog
- Python
- Quackery
- R
- Racket
- Raku
- Red
- REXX
- Ring
- RPL
- Ruby
- Rust
- Scala
- Seed7
- Sidef
- Swift
- Tcl
- Transd
- Uiua
- Vala
- V (Vlang)
- Wren
- Wren-math
- XPL0
- Zkl