Sequence: smallest number greater than previous term with exactly n divisors: Difference between revisions
Realize in F# |
m →{{header|Wren}}: Minor tidy |
||
(28 intermediate revisions by 21 users not shown) | |||
Line 18: | Line 18: | ||
:* [[Sequence: nth number with exactly n divisors]] |
:* [[Sequence: nth number with exactly n divisors]] |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="11l">F divisors(n) |
|||
V divs = [1] |
|||
L(ii) 2 .< Int(n ^ 0.5) + 3 |
|||
I n % ii == 0 |
|||
divs.append(ii) |
|||
divs.append(Int(n / ii)) |
|||
divs.append(n) |
|||
R Array(Set(divs)) |
|||
F sequence(max_n) |
|||
V previous = 0 |
|||
V n = 0 |
|||
[Int] r |
|||
L |
|||
n++ |
|||
V ii = previous |
|||
I n > max_n |
|||
L.break |
|||
L |
|||
ii++ |
|||
I divisors(ii).len == n |
|||
r.append(ii) |
|||
previous = ii |
|||
L.break |
|||
R r |
|||
L(item) sequence(15) |
|||
print(item)</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1 |
|||
2 |
|||
4 |
|||
6 |
|||
16 |
|||
18 |
|||
64 |
|||
66 |
|||
100 |
|||
112 |
|||
1024 |
|||
1035 |
|||
4096 |
|||
4288 |
|||
4624 |
|||
</pre> |
|||
=={{header|Action!}}== |
|||
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU. |
|||
<syntaxhighlight lang="action!">CARD FUNC CountDivisors(CARD a) |
|||
CARD i,count |
|||
i=1 count=0 |
|||
WHILE i*i<=a |
|||
DO |
|||
IF a MOD i=0 THEN |
|||
IF i=a/i THEN |
|||
count==+1 |
|||
ELSE |
|||
count==+2 |
|||
FI |
|||
FI |
|||
i==+1 |
|||
OD |
|||
RETURN (count) |
|||
PROC Main() |
|||
CARD a |
|||
BYTE i |
|||
a=1 |
|||
FOR i=1 TO 15 |
|||
DO |
|||
WHILE CountDivisors(a)#i |
|||
DO |
|||
a==+1 |
|||
OD |
|||
IF i>1 THEN |
|||
Print(", ") |
|||
FI |
|||
PrintC(a) |
|||
OD |
|||
RETURN</syntaxhighlight> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sequence_smallest_number_greater_than_previous_term_with_exactly_n_divisors.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624 |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Show_Sequence is |
procedure Show_Sequence is |
||
Line 60: | Line 153: | ||
begin |
begin |
||
Show (15); |
Show (15); |
||
end Show_Sequence;</ |
end Show_Sequence;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 67: | Line 160: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{trans|Go}} |
{{trans|Go}} with a small optimisation. |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
PROC count divisors = ( INT n )INT: |
PROC count divisors = ( INT n )INT: |
||
BEGIN |
BEGIN |
||
INT count := 0; |
INT i2, count := 0; |
||
FOR i WHILE i*i < |
FOR i WHILE ( i2 := i * i ) < n DO |
||
IF n MOD i = 0 THEN |
IF n MOD i = 0 THEN count +:= 2 FI |
||
count +:= IF i = n OVER i THEN 1 ELSE 2 FI |
|||
FI |
|||
OD; |
OD; |
||
count |
IF i2 = n THEN count + 1 ELSE count FI |
||
END # count divisors # ; |
END # count divisors # ; |
||
INT max = 15; |
INT max = 15; |
||
print( ( "The first ", whole( max, 0 ), " terms of the sequence are:" |
print( ( "The first ", whole( max, 0 ), " terms of the sequence are:" ) ); |
||
INT next := 1; |
INT next := 1; |
||
FOR i WHILE next <= max DO |
FOR i WHILE next <= max DO |
||
IF next = count divisors( i ) THEN |
IF next = count divisors( i ) THEN |
||
print( ( whole( i, 0 ) |
print( ( " ", whole( i, 0 ) ) ); |
||
next +:= 1 |
next +:= 1 |
||
FI |
FI |
||
OD |
OD |
||
print( ( newline, newline ) ) |
|||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
The first 15 terms of the sequence are: |
The first 15 terms of the sequence are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
||
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
|||
</pre> |
</pre> |
||
=={{header|ALGOL W}}== |
|||
{{Trans|Go}}...via Algol 68 and with a small optimisation. |
|||
<syntaxhighlight lang="pascal">begin |
|||
integer max, next, i; |
|||
integer procedure countDivisors ( Integer value n ) ; |
|||
begin |
|||
integer count, i, i2; |
|||
count := 0; |
|||
i := 1; |
|||
while begin i2 := i * i; |
|||
i2 < n |
|||
end do begin |
|||
if n rem i = 0 then count := count + 2; |
|||
i := i + 1 |
|||
end; |
|||
if i2 = n then count + 1 else count |
|||
end countDivisors ; |
|||
max := 15; |
|||
write( i_w := 1, s_w := 0, "The first ", max, " terms of the sequence are: " ); |
|||
i := next := 1; |
|||
while next <= max do begin |
|||
if next = countDivisors( i ) then begin |
|||
writeon( i_w := 1, s_w := 0, " ", i ); |
|||
next := next + 1 |
|||
end; |
|||
i := i + 1 |
|||
end; |
|||
write() |
|||
end.</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 15 terms of the sequence are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
|||
</pre> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">i: new 0 |
|||
next: new 1 |
|||
MAX: 15 |
|||
while [next =< MAX][ |
|||
if next = size factors i [ |
|||
prints ~"|i| " |
|||
inc 'next |
|||
] |
|||
inc 'i |
|||
] |
|||
print ""</syntaxhighlight> |
|||
{{out}} |
|||
<pre>1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624</pre> |
|||
=={{header|AutoHotkey}}== |
|||
{{trans|Go}} |
|||
<syntaxhighlight lang="autohotkey">MAX := 15 |
|||
next := 1, i := 1 |
|||
while (next <= MAX) |
|||
if (next = countDivisors(A_Index)) |
|||
Res.= A_Index ", ", next++ |
|||
MsgBox % "The first " MAX " terms of the sequence are:`n" Trim(res, ", ") |
|||
return |
|||
countDivisors(n){ |
|||
while (A_Index**2 <= n) |
|||
if !Mod(n, A_Index) |
|||
count += (A_Index = n/A_Index) ? 1 : 2 |
|||
return count |
|||
}</syntaxhighlight> |
|||
Outputs:<pre>The first 15 terms of the sequence are: |
|||
1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f SEQUENCE_SMALLEST_NUMBER_GREATER_THAN_PREVIOUS_TERM_WITH_EXACTLY_N_DIVISORS.AWK |
# syntax: GAWK -f SEQUENCE_SMALLEST_NUMBER_GREATER_THAN_PREVIOUS_TERM_WITH_EXACTLY_N_DIVISORS.AWK |
||
# converted from Kotlin |
# converted from Kotlin |
||
Line 125: | Line 287: | ||
return(count) |
return(count) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
first 15 terms: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
first 15 terms: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
||
</pre> |
</pre> |
||
=={{header|BASIC}}== |
|||
==={{header|BASIC256}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="vbnet">UPTO = 15 |
|||
i = 2 |
|||
nfound = 1 |
|||
print 1; " "; #special case |
|||
while nfound < UPTO |
|||
n = divisors(i) |
|||
if n = nfound + 1 then |
|||
nfound += 1 |
|||
print i; " "; |
|||
end if |
|||
i += 1 |
|||
end while |
|||
end |
|||
function divisors(n) |
|||
#find the number of divisors of an integer |
|||
r = 2 |
|||
for i = 2 to n\2 |
|||
if n mod i = 0 then r += 1 |
|||
next i |
|||
return r |
|||
end function</syntaxhighlight> |
|||
==={{header|Gambas}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="vbnet">Public Sub Main() |
|||
Dim UPTO As Integer = 15, i As Integer = 2 |
|||
Dim n As Integer, nfound As Integer = 1 |
|||
Print 1; " "; 'special case |
|||
While nfound < UPTO |
|||
n = divisors(i) |
|||
If n = nfound + 1 Then |
|||
nfound += 1 |
|||
Print i; " "; |
|||
End If |
|||
i += 1 |
|||
Wend |
|||
End Sub |
|||
Function divisors(n As Integer) As Integer |
|||
'find the number of divisors of an integer |
|||
Dim r As Integer = 2, i As Integer |
|||
For i = 2 To n \ 2 |
|||
If n Mod i = 0 Then r += 1 |
|||
Next |
|||
Return r |
|||
End Function</syntaxhighlight> |
|||
==={{header|PureBasic}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="purebasic">Procedure.i divisors(n) |
|||
;find the number of divisors of an integer |
|||
Define.i r, i |
|||
r = 2 |
|||
For i = 2 To n/2 |
|||
If n % i = 0: r + 1 |
|||
EndIf |
|||
Next i |
|||
ProcedureReturn r |
|||
EndProcedure |
|||
OpenConsole() |
|||
Define.i UPTO, i, n, found |
|||
UPTO = 15 |
|||
i = 2 |
|||
nfound = 1 |
|||
Print("1 ") ;special case |
|||
While nfound < UPTO |
|||
n = divisors(i) |
|||
If n = nfound + 1: |
|||
nfound + 1 |
|||
Print(Str(i) + " ") |
|||
EndIf |
|||
i + 1 |
|||
Wend |
|||
PrintN(#CRLF$ + "Press ENTER to exit"): Input() |
|||
CloseConsole()</syntaxhighlight> |
|||
==={{header|QBasic}}=== |
|||
{{trans|FreeBASIC}} |
|||
{{works with|QBasic|1.1}} |
|||
{{works with|QuickBasic|4.5}} |
|||
<syntaxhighlight lang="qbasic">FUNCTION divisors (n) |
|||
'find the number of divisors of an integer |
|||
r = 2 |
|||
FOR i = 2 TO n \ 2 |
|||
IF n MOD i = 0 THEN r = r + 1 |
|||
NEXT i |
|||
divisors = r |
|||
END FUNCTION |
|||
UPTO = 15 |
|||
i = 2 |
|||
nfound = 1 |
|||
PRINT 1; 'special case |
|||
WHILE nfound < UPTO |
|||
n = divisors(i) |
|||
IF n = nfound + 1 THEN |
|||
nfound = nfound + 1 |
|||
PRINT i; |
|||
END IF |
|||
i = i + 1 |
|||
WEND</syntaxhighlight> |
|||
==={{header|Run BASIC}}=== |
|||
{{trans|FreeBASIC}} |
|||
{{works with|Just BASIC}} |
|||
{{works with|Liberty BASIC}} |
|||
<syntaxhighlight lang="vb">UPTO = 15 |
|||
i = 2 |
|||
nfound = 1 |
|||
print 1; " "; 'special case |
|||
while nfound < UPTO |
|||
n = divisors(i) |
|||
if n = nfound + 1 then |
|||
nfound = nfound + 1 |
|||
print i; " "; |
|||
end if |
|||
i = i + 1 |
|||
wend |
|||
print |
|||
end |
|||
function divisors(n) |
|||
'find the number of divisors of an integer |
|||
r = 2 |
|||
for i = 2 to n / 2 |
|||
if n mod i = 0 then r = r + 1 |
|||
next i |
|||
divisors = r |
|||
end function</syntaxhighlight> |
|||
==={{header|XBasic}}=== |
|||
{{trans|FreeBASIC}} |
|||
{{works with|Windows XBasic}} |
|||
<syntaxhighlight lang="qbasic">PROGRAM "program name" |
|||
VERSION "0.0000" |
|||
DECLARE FUNCTION Entry () |
|||
DECLARE FUNCTION divisors (n) |
|||
FUNCTION Entry () |
|||
UPTO = 15 |
|||
i = 2 |
|||
nfound = 1 |
|||
PRINT 1; 'special case |
|||
DO WHILE nfound < UPTO |
|||
n = divisors(i) |
|||
IF n = nfound + 1 THEN |
|||
INC nfound |
|||
PRINT i; |
|||
END IF |
|||
INC i |
|||
LOOP |
|||
END FUNCTION |
|||
FUNCTION divisors (n) |
|||
'find the number of divisors of an integer |
|||
r = 2 |
|||
FOR i = 2 TO n / 2 |
|||
IF n MOD i = 0 THEN INC r |
|||
NEXT i |
|||
RETURN r |
|||
END FUNCTION |
|||
END PROGRAM</syntaxhighlight> |
|||
==={{header|Yabasic}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="vbnet">UPTO = 15 |
|||
i = 2 |
|||
nfound = 1 |
|||
print 1, " "; //special case |
|||
while nfound < UPTO |
|||
n = divisors(i) |
|||
if n = nfound + 1 then |
|||
nfound = nfound + 1 |
|||
print i, " "; |
|||
fi |
|||
i = i + 1 |
|||
end while |
|||
print |
|||
end |
|||
sub divisors(n) |
|||
local r, i |
|||
//find the number of divisors of an integer |
|||
r = 2 |
|||
for i = 2 to n / 2 |
|||
if mod(n, i) = 0 r = r + 1 |
|||
next i |
|||
return r |
|||
end sub</syntaxhighlight> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#define MAX 15 |
#define MAX 15 |
||
Line 161: | Line 540: | ||
printf("\n"); |
printf("\n"); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 171: | Line 550: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#define MAX 15 |
#define MAX 15 |
||
Line 200: | Line 579: | ||
cout << endl; |
cout << endl; |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 209: | Line 588: | ||
===Alternative=== |
===Alternative=== |
||
{{Trans|Pascal}} |
{{Trans|Pascal}} |
||
More terms and quicker than the straightforward C version above. [https://tio.run/##bVJNb9NAFLz7V0yLGu1ihzopJ38EIbgiJMSBG3J3N84KZ@1417QI5a8T3tuQNqm42W/emxnPWA3DvFXqcHhlneombVApH7TtV8nZZDP2jibJ5K1r4Zqt8UOjDGizKI5omSSqdz7AuoBP77@hxt2yTJBMztvWGR2B1oSP9qfVZvQfXBAXmJP4nQAXM800GXYZ9BfjM4zG02RBvHjY2M5AXAmHGRaSjuGwWjHKa2laYv@8JjReQ0tUddTBjmgcbqFL2DWI44a1auQMshbhOcsAuj@N0pqZSzqssSuJ47Zmgv1J5IzleIpomM74nBc1vyz5aTRhGh1RXZFjvIuLVUWPxVFjnyQcwLaxTrCni1hsBvcYMlCWFCNrDSPN1@L668ZgbUfq4UajccHOCaG6MHSTRzOaAtcZ9xMdNlPoqUT6HB9Mo399V12vfhSF6x@EpGQ4pMcQE48xxI/ivMRRmZAXjVoqoj5eseuTLTJDsvYplsiRpry2wluJ2QziBRNhkWop5b8jRENiwTkxjHnsfc7u9rCx8acqCKauTx965mPryYmgVyn0NDbB9q7S/XTfmZX4TwpE74N8o/qJPEn6iRZ5nksi3R8Of9S6a1p/mH@@@ws Try It Online!]< |
More terms and quicker than the straightforward C version above. [https://tio.run/##bVJNb9NAFLz7V0yLGu1ihzopJ38EIbgiJMSBG3J3N84KZ@1417QI5a8T3tuQNqm42W/emxnPWA3DvFXqcHhlneombVApH7TtV8nZZDP2jibJ5K1r4Zqt8UOjDGizKI5omSSqdz7AuoBP77@hxt2yTJBMztvWGR2B1oSP9qfVZvQfXBAXmJP4nQAXM800GXYZ9BfjM4zG02RBvHjY2M5AXAmHGRaSjuGwWjHKa2laYv@8JjReQ0tUddTBjmgcbqFL2DWI44a1auQMshbhOcsAuj@N0pqZSzqssSuJ47Zmgv1J5IzleIpomM74nBc1vyz5aTRhGh1RXZFjvIuLVUWPxVFjnyQcwLaxTrCni1hsBvcYMlCWFCNrDSPN1@L668ZgbUfq4UajccHOCaG6MHSTRzOaAtcZ9xMdNlPoqUT6HB9Mo399V12vfhSF6x@EpGQ4pMcQE48xxI/ivMRRmZAXjVoqoj5eseuTLTJDsvYplsiRpry2wluJ2QziBRNhkWop5b8jRENiwTkxjHnsfc7u9rCx8acqCKauTx965mPryYmgVyn0NDbB9q7S/XTfmZX4TwpE74N8o/qJPEn6iRZ5nksi3R8Of9S6a1p/mH@@@ws Try It Online!]<syntaxhighlight lang="cpp">#include <cstdio> |
||
#include <chrono> |
#include <chrono> |
||
Line 230: | Line 609: | ||
i = (1 << (nxt - 1)) - 1; } i++; } while (nxt <= MAX); |
i = (1 << (nxt - 1)) - 1; } i++; } while (nxt <= MAX); |
||
printf("%d ms", (int)(duration<double>(steady_clock::now() - st).count() * 1000)); |
printf("%d ms", (int)(duration<double>(steady_clock::now() - st).count() * 1000)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The first 32 anti-primes plus are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 4632 65536 65572 262144 262192 263169 269312 4194304 4194306 4477456 4493312 4498641 4498752 268435456 268437200 1073741824 1073741830 223 ms</pre> |
<pre>The first 32 anti-primes plus are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 4632 65536 65572 262144 262192 263169 269312 4194304 4194306 4477456 4493312 4498641 4498752 268435456 268437200 1073741824 1073741830 223 ms</pre> |
||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
<syntaxhighlight lang="Delphi"> |
|||
{These routines would normally be in a library, but the shown here for clarity} |
|||
function GetAllProperDivisors(N: Integer;var IA: TIntegerDynArray): integer; |
|||
{Make a list of all the "proper dividers" for N} |
|||
{Proper dividers are the of numbers the divide evenly into N} |
|||
var I: integer; |
|||
begin |
|||
SetLength(IA,0); |
|||
for I:=1 to N-1 do |
|||
if (N mod I)=0 then |
|||
begin |
|||
SetLength(IA,Length(IA)+1); |
|||
IA[High(IA)]:=I; |
|||
end; |
|||
Result:=Length(IA); |
|||
end; |
|||
function GetAllDivisors(N: Integer;var IA: TIntegerDynArray): integer; |
|||
{Make a list of all the "proper dividers" for N, Plus N itself} |
|||
begin |
|||
Result:=GetAllProperDivisors(N,IA)+1; |
|||
SetLength(IA,Length(IA)+1); |
|||
IA[High(IA)]:=N; |
|||
end; |
|||
procedure SmallestWithNDivisors(Memo: TMemo); |
|||
var N,Count: integer; |
|||
var IA: TIntegerDynArray; |
|||
begin |
|||
Count:=1; |
|||
for N:=1 to high(Integer) do |
|||
if Count=GetAllDivisors(N,IA) then |
|||
begin |
|||
Memo.Lines.Add(IntToStr(Count)+' - '+IntToStr(N)); |
|||
Inc(Count); |
|||
if Count>15 then break; |
|||
end; |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1 - 1 |
|||
2 - 2 |
|||
3 - 4 |
|||
4 - 6 |
|||
5 - 16 |
|||
6 - 18 |
|||
7 - 64 |
|||
8 - 66 |
|||
9 - 100 |
|||
10 - 112 |
|||
11 - 1024 |
|||
12 - 1035 |
|||
13 - 4096 |
|||
14 - 4288 |
|||
15 - 4624 |
|||
Elapsed Time: 58.590 ms. |
|||
</pre> |
|||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |
||
Line 238: | Line 691: | ||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="dyalect">func countDivisors(n) { |
||
var count = 0 |
var count = 0 |
||
var i = 1 |
var i = 1 |
||
Line 254: | Line 707: | ||
} |
} |
||
let max = 15 |
|||
print("The first \(max) terms of the sequence are:") |
print("The first \(max) terms of the sequence are:") |
||
var (i, next) = (1, 1) |
var (i, next) = (1, 1) |
||
while next <= max { |
while next <= max { |
||
if next == countDivisors(i) { |
if next == countDivisors(i) { |
||
print("\(i) ", terminator |
print("\(i) ", terminator: "") |
||
next += 1 |
next += 1 |
||
} |
} |
||
Line 265: | Line 718: | ||
} |
} |
||
print()</ |
print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 271: | Line 724: | ||
<pre>The first 15 terms of the sequence are: |
<pre>The first 15 terms of the sequence are: |
||
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624</pre> |
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624</pre> |
||
=={{header|EasyLang}}== |
|||
{{trans|AWK}} |
|||
<syntaxhighlight> |
|||
func ndivs n . |
|||
i = 1 |
|||
while i <= sqrt n |
|||
if n mod i = 0 |
|||
cnt += 2 |
|||
if i = n div i |
|||
cnt -= 1 |
|||
. |
|||
. |
|||
i += 1 |
|||
. |
|||
return cnt |
|||
. |
|||
n = 1 |
|||
while n <= 15 |
|||
i += 1 |
|||
if n = ndivs i |
|||
write i & " " |
|||
n += 1 |
|||
. |
|||
. |
|||
</syntaxhighlight> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
===First 28 are easy with a Naive implementation=== |
===First 28 are easy with a Naive implementation=== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Nigel Galloway: November 19th., 2017 |
// Nigel Galloway: November 19th., 2017 |
||
let fN g=[1..(float>>sqrt>>int)g]|>List.fold(fun Σ n->if g%n>0 then Σ else if g/n=n then Σ+1 else Σ+2) 0 |
let fN g=[1..(float>>sqrt>>int)g]|>List.fold(fun Σ n->if g%n>0 then Σ else if g/n=n then Σ+1 else Σ+2) 0 |
||
Line 280: | Line 759: | ||
A069654 |> Seq.take 28|>Seq.iter(printf "%d "); printfn "" |
A069654 |> Seq.take 28|>Seq.iter(printf "%d "); printfn "" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 4632 65536 65572 262144 262192 263169 269312 4194304 4194306 4477456 4493312 4498641 4498752 |
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 4632 65536 65572 262144 262192 263169 269312 4194304 4194306 4477456 4493312 4498641 4498752 |
||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: io kernel math math.primes.factors prettyprint sequences ; |
||
: next ( n num -- n' num' ) |
: next ( n num -- n' num' ) |
||
Line 294: | Line 774: | ||
[ 2 1 ] dip [ [ next ] keep ] replicate 2nip ; |
[ 2 1 ] dip [ [ next ] keep ] replicate 2nip ; |
||
"The first 15 terms of the sequence are:" print 15 A069654 .</ |
"The first 15 terms of the sequence are:" print 15 A069654 .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 300: | Line 780: | ||
{ 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 } |
{ 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 } |
||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic">#define UPTO 15 |
|||
function divisors(byval n as ulongint) as uinteger |
|||
'find the number of divisors of an integer |
|||
dim as integer r = 2, i |
|||
for i = 2 to n\2 |
|||
if n mod i = 0 then r += 1 |
|||
next i |
|||
return r |
|||
end function |
|||
dim as ulongint i = 2 |
|||
dim as integer n, nfound = 1 |
|||
print 1;" "; 'special case |
|||
while nfound < UPTO |
|||
n = divisors(i) |
|||
if n = nfound + 1 then |
|||
nfound += 1 |
|||
print i;" "; |
|||
end if |
|||
i+=1 |
|||
wend |
|||
print |
|||
end</syntaxhighlight> |
|||
{{out}}<pre> 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 330: | Line 839: | ||
} |
} |
||
fmt.Println() |
fmt.Println() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 338: | Line 847: | ||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Text.Printf (printf) |
||
sequence_A069654 :: [(Int,Int)] |
sequence_A069654 :: [(Int,Int)] |
||
Line 349: | Line 858: | ||
main :: IO () |
main :: IO () |
||
main = mapM_ (uncurry $ printf "a(%2d)=%5d\n") $ take 15 sequence_A069654</ |
main = mapM_ (uncurry $ printf "a(%2d)=%5d\n") $ take 15 sequence_A069654</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>a( 1)= 1 |
<pre>a( 1)= 1 |
||
Line 369: | Line 878: | ||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang="j"> |
|||
<lang j> |
|||
sieve=: 3 :0 |
sieve=: 3 :0 |
||
NB. sieve y returns a vector of y boxes. |
NB. sieve y returns a vector of y boxes. |
||
Line 387: | Line 896: | ||
(</.~ {."1) (,. i.@:#)tally |
(</.~ {."1) (,. i.@:#)tally |
||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 515: | Line 1,024: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="java">public class AntiPrimesPlus { |
||
static int count_divisors(int n) { |
static int count_divisors(int n) { |
||
Line 541: | Line 1,050: | ||
System.out.println(); |
System.out.println(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 548: | Line 1,057: | ||
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
||
</pre> |
</pre> |
||
=={{header|jq}}== |
|||
'''Adapted from [[Julia]]''' |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
<syntaxhighlight lang="jq"> |
|||
# The number of prime factors (as in prime factorization) |
|||
def numfactors: |
|||
. as $num |
|||
| reduce range(1; 1 + sqrt|floor) as $i (null; |
|||
if ($num % $i) == 0 |
|||
then ($num / $i) as $r |
|||
| if $i == $r then .+1 else .+2 end |
|||
else . |
|||
end ); |
|||
# Output: a stream |
|||
def A06954: |
|||
foreach range(1; infinite) as $i ({k: 0}; |
|||
.j = .k + 1 |
|||
| until( $i == (.j | numfactors); .j += 1) |
|||
| .k = .j ; |
|||
.j ) ; |
|||
"First 15 terms of OEIS sequence A069654: ", |
|||
[limit(15; A06954)]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 15 terms of OEIS sequence A069654: |
|||
[1,2,4,6,16,18,64,66,100,112,1024,1035,4096,4288,4624] |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
function numfactors(n) |
function numfactors(n) |
||
Line 577: | Line 1,118: | ||
A06954(15) |
A06954(15) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
First 15 terms of OEIS sequence A069654: |
First 15 terms of OEIS sequence A069654: |
||
Line 585: | Line 1,126: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="scala">// Version 1.3.21 |
||
const val MAX = 15 |
const val MAX = 15 |
||
Line 613: | Line 1,154: | ||
} |
} |
||
println() |
println() |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 620: | Line 1,161: | ||
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
||
</pre> |
</pre> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
|||
<syntaxhighlight lang="mathematica">res = {#, DivisorSigma[0, #]} & /@ Range[100000]; |
|||
highest = 0; |
|||
filter = {}; |
|||
Do[ |
|||
If[r[[2]] == highest + 1, |
|||
AppendTo[filter, r[[1]]]; |
|||
highest = r[[2]] |
|||
] |
|||
, |
|||
{r, res} |
|||
] |
|||
Take[filter, 15]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>{1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624}</pre> |
|||
=={{header|Maxima}}== |
|||
<syntaxhighlight lang="maxima"> |
|||
sngptend(n):=block([i:1,count:1,result:[]], |
|||
while count<=n do (if length(listify(divisors(i)))=count then (result:endcons(i,result),count:count+1),i:i+1), |
|||
result)$ |
|||
/* Test case */ |
|||
sngptend(15); |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[1,2,4,6,16,18,64,66,100,112,1024,1035,4096,4288,4624] |
|||
</pre> |
|||
=={{header|MiniScript}}== |
|||
This GUI implementation is for use with [http://miniscript.org/MiniMicro Mini Micro]. |
|||
<syntaxhighlight lang="miniscript"> |
|||
divisors = function(n) |
|||
divs = {1: 1} |
|||
divs[n] = 1 |
|||
i = 2 |
|||
while i * i <= n |
|||
if n % i == 0 then |
|||
divs[i] = 1 |
|||
divs[n / i] = 1 |
|||
end if |
|||
i += 1 |
|||
end while |
|||
return divs.indexes |
|||
end function |
|||
counts = [] |
|||
j = 1 |
|||
for i in range(1, 15) |
|||
while divisors(j).len != i |
|||
j += 1 |
|||
end while |
|||
counts.push(j) |
|||
end for |
|||
print "The first 15 terms in the sequence are:" |
|||
print counts.join(", ") |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 15 terms in the sequence are: |
|||
1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624</pre> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strformat |
||
const MAX = 15 |
const MAX = 15 |
||
Line 645: | Line 1,250: | ||
inc next |
inc next |
||
inc i |
inc i |
||
write(stdout, "\n")</ |
write(stdout, "\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 658: | Line 1,263: | ||
I think of next = 33 aka 11*3 with the solution 1031^2 * 2^10=1,088,472,064 with a big distance to next= 32 => 1073741830.<BR> |
I think of next = 33 aka 11*3 with the solution 1031^2 * 2^10=1,088,472,064 with a big distance to next= 32 => 1073741830.<BR> |
||
[https://tio.run/##fVRdb9owFH3Pr7gPk0hYWCG0e2hKJcbHhtQCapm0vSCZxAGvwaa2U1ZV/PWxazsQ2k7jIeR@n3t8HLH4RRPd2BCVkLyRbZL9fiPFUpI1dLlmU8nWVE3zQsXey4fRsD8YwnDa23kALx9uJ/0B9Gm@WbEdRgc39wMX6E6ns5/TAfQm4/vJzWAHZ2eQ2jyTNu6PhjuvUFRhsnpWhWa5CtdEr2IvEVxpdN92f0CnHcUeeFnBE80EhyXVffbEUipVj2ufX35nXLejoPyPPZzyOgcWz5Aak/ElcMA0AUwr2JitICOJFlKZMvJAMN6BKLpomr95q96eR/WLeRtWRIHf@tgK6n5kn218Ysq564yDvCciEbIxw8dC6BBX1eSOqlBSpeESDvgWdMk4ZqK7yDHQgZZZEJAeUwzGE8VoKbohkmjYMr0ShbatFVKAqdsVyymMhfYnaerzIIBUoPtL2RpwDezC4f7bne2OP8YT340MjIPy9DA1k2INXGwB2RU8fwaRpsCL9cJs5TayoNrxcbKvHqVvAsFVh78bjtzfCoQVVmQYDoISSAaWkQ40Qa@oK6mKAQ7EmZnNuHRKuqFEl4bb5pRgu5YLHXMsB2b628D/8QEUKPncgby6riBUDB7PtixxZB5gmabRCclIMe6cE2znxAYiMzosFYgJoyHaOKpVEVKpw73VUTnlmTmhzZqoqRHXn8/NCBZy@luHuBkK/r3WtpJpmnO/Nluh4plEKLUQb1dYA4LLNiwQxIM3HIiklzULf9bE8V@pnrHkoSeK46yDZsHMPBonJ@RgmMib28pKwnBhl9NxPf6pAwvaZ4ixFpyegak4Oiy3tseBT9vMCdnaPnSuYS2URkcEwdzWN1qBKVAiL7S7UiUsG4VrOA@gO@6D//ZzY2abb0NQYba4LA2gVjkc@zdaMX5UmJmDsCVdU65pCiTTVG6JTNV77Th@nP7GDgieUlyd4Mmr//pkGrMmMrVWhivs@cnb7/8kWU6Wat@YRH8B Try it online!] |
[https://tio.run/##fVRdb9owFH3Pr7gPk0hYWCG0e2hKJcbHhtQCapm0vSCZxAGvwaa2U1ZV/PWxazsQ2k7jIeR@n3t8HLH4RRPd2BCVkLyRbZL9fiPFUpI1dLlmU8nWVE3zQsXey4fRsD8YwnDa23kALx9uJ/0B9Gm@WbEdRgc39wMX6E6ns5/TAfQm4/vJzWAHZ2eQ2jyTNu6PhjuvUFRhsnpWhWa5CtdEr2IvEVxpdN92f0CnHcUeeFnBE80EhyXVffbEUipVj2ufX35nXLejoPyPPZzyOgcWz5Aak/ElcMA0AUwr2JitICOJFlKZMvJAMN6BKLpomr95q96eR/WLeRtWRIHf@tgK6n5kn218Ysq564yDvCciEbIxw8dC6BBX1eSOqlBSpeESDvgWdMk4ZqK7yDHQgZZZEJAeUwzGE8VoKbohkmjYMr0ShbatFVKAqdsVyymMhfYnaerzIIBUoPtL2RpwDezC4f7bne2OP8YT340MjIPy9DA1k2INXGwB2RU8fwaRpsCL9cJs5TayoNrxcbKvHqVvAsFVh78bjtzfCoQVVmQYDoISSAaWkQ40Qa@oK6mKAQ7EmZnNuHRKuqFEl4bb5pRgu5YLHXMsB2b628D/8QEUKPncgby6riBUDB7PtixxZB5gmabRCclIMe6cE2znxAYiMzosFYgJoyHaOKpVEVKpw73VUTnlmTmhzZqoqRHXn8/NCBZy@luHuBkK/r3WtpJpmnO/Nluh4plEKLUQb1dYA4LLNiwQxIM3HIiklzULf9bE8V@pnrHkoSeK46yDZsHMPBonJ@RgmMib28pKwnBhl9NxPf6pAwvaZ4ixFpyegak4Oiy3tseBT9vMCdnaPnSuYS2URkcEwdzWN1qBKVAiL7S7UiUsG4VrOA@gO@6D//ZzY2abb0NQYba4LA2gVjkc@zdaMX5UmJmDsCVdU65pCiTTVG6JTNV77Th@nP7GDgieUlyd4Mmr//pkGrMmMrVWhivs@cnb7/8kWU6Wat@YRH8B Try it online!] |
||
< |
<syntaxhighlight lang="pascal">program AntiPrimesPlus; |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
{$MODE Delphi} |
{$MODE Delphi} |
||
Line 728: | Line 1,333: | ||
writeln; |
writeln; |
||
writeln(GetTickCount64-T0,' ms'); |
writeln(GetTickCount64-T0,' ms'); |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The first 32 anti-primes plus are: |
<pre>The first 32 anti-primes plus are: |
||
Line 736: | Line 1,341: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use ntheory 'divisors'; |
use ntheory 'divisors'; |
||
Line 747: | Line 1,352: | ||
print("$l "), $m = $l, last if $n == divisors($l); |
print("$l "), $m = $l, last if $n == divisors($l); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 755: | Line 1,360: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Uses the optimisation trick from pascal, of n:=power(2,next-1) when next is a prime>4. |
Uses the optimisation trick from pascal, of n:=power(2,next-1) when next is a prime>4. |
||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<lang Phix>constant limit = 15 |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
sequence res = repeat(0,limit) |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">15</span> |
|||
integer next = 1 |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span> |
|||
atom n = 1 |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">next</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
|||
while next<=limit do |
|||
<span style="color: #004080;">atom</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
|||
integer k = length(factors(n,1)) |
|||
<span style="color: #008080;">while</span> <span style="color: #000000;">next</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">limit</span> <span style="color: #008080;">do</span> |
|||
if k=next then |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span> |
|||
res[k] = n |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">next</span> <span style="color: #008080;">then</span> |
|||
next += 1 |
|||
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span> |
|||
if next>4 and is_prime(next) then |
|||
<span style="color: #000000;">next</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> |
|||
n := power(2,next-1)-1 -- (-1 for +=1 next) |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">next</span><span style="color: #0000FF;">></span><span style="color: #000000;">4</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">next</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> |
|||
end if |
|||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">:=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">next</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- (-1 for +=1 next)</span> |
|||
end if |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
n += 1 |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
end while |
|||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> |
|||
printf(1,"The first %d terms are: %v\n",{limit,res})</lang> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first %d terms are: %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
The first 15 terms are: {1,2,4,6,16,18,64,66,100,112,1024,1035,4096,4288,4624} |
The first 15 terms are: {1,2,4,6,16,18,64,66,100,112,1024,1035,4096,4288,4624} |
||
</pre> |
|||
You can raise the limit to 32, the 25th takes about 4s but the rest are all near-instant, however I lost patience waiting for the 33rd.<br> |
|||
<small>(The last two trailing .0 just mean "not a 31-bit integer" and don't appear when run on 64 bit.)</small> |
|||
<pre> |
|||
The first 32 terms are: {1,2,4,6,16,18,64,66,100,112,1024,1035,4096,4288,4624, |
|||
4632,65536,65572,262144,262192,263169,269312,4194304, |
|||
4194306,4477456,4493312,4498641,4498752,268435456, |
|||
268437200,1073741824.0,1073741830.0} |
|||
</pre> |
|||
=={{header|PL/I}}== |
|||
See [[#Polyglot:PL/I and PL/M]] |
|||
=={{header|PL/M}}== |
|||
{{Trans|Go}} via Algol 68 |
|||
<syntaxhighlight lang="pli">100H: /* FIND THE SMALLEST NUMBER > THE PREVIOUS ONE WITH EXACTLY N DIVISORS */ |
|||
/* CP/M BDOS SYSTEM CALL */ |
|||
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END; |
|||
/* CONSOLE OUTPUT ROUTINES */ |
|||
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END; |
|||
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END; |
|||
PR$NL: PROCEDURE; CALL PR$STRING( .( 0DH, 0AH, '$' ) ); END; |
|||
PR$NUMBER: PROCEDURE( N ); |
|||
DECLARE N ADDRESS; |
|||
DECLARE V ADDRESS, N$STR( 6 ) BYTE INITIAL( '.....$' ), W BYTE; |
|||
N$STR( W := LAST( N$STR ) - 1 ) = '0' + ( ( V := N ) MOD 10 ); |
|||
DO WHILE( ( V := V / 10 ) > 0 ); |
|||
N$STR( W := W - 1 ) = '0' + ( V MOD 10 ); |
|||
END; |
|||
CALL PR$STRING( .N$STR( W ) ); |
|||
END PR$NUMBER; |
|||
/* TASK */ |
|||
/* RETURNS THE DIVISOR COUNT OF N */ |
|||
COUNT$DIVISORS: PROCEDURE( N )ADDRESS; |
|||
DECLARE N ADDRESS; |
|||
DECLARE ( I, I2, COUNT ) ADDRESS; |
|||
COUNT = 0; |
|||
I = 1; |
|||
DO WHILE( ( I2 := I * I ) < N ); |
|||
IF N MOD I = 0 THEN COUNT = COUNT + 2; |
|||
I = I + 1; |
|||
END; |
|||
IF I2 = N THEN RETURN ( COUNT + 1 ); ELSE RETURN ( COUNT ); |
|||
END COUNT$DIVISORS ; |
|||
DECLARE MAX LITERALLY '15'; |
|||
DECLARE ( I, NEXT ) ADDRESS; |
|||
CALL PR$STRING( .'THE FIRST $' ); |
|||
CALL PR$NUMBER( MAX ); |
|||
CALL PR$STRING( .' TERMS OF THE SEQUENCE ARE:$' ); |
|||
NEXT = 1; |
|||
I = 1; |
|||
DO WHILE( NEXT <= MAX ); |
|||
IF NEXT = COUNT$DIVISORS( I ) THEN DO; |
|||
CALL PR$CHAR( ' ' ); |
|||
CALL PR$NUMBER( I ); |
|||
NEXT = NEXT + 1; |
|||
END; |
|||
I = I + 1; |
|||
END; |
|||
EOF</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
THE FIRST 15 TERMS OF THE SEQUENCE ARE: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
|||
</pre> |
|||
See also [[#Polyglot:PL/I and PL/M]] |
|||
=={{header|Polyglot:PL/I and PL/M}}== |
|||
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator) |
|||
Should work with many PL/I implementations. |
|||
<br> |
|||
The PL/I include file "pg.inc" can be found on the [[Polyglot:PL/I and PL/M]] page. |
|||
Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler. |
|||
{{Trans|PL/M}} |
|||
<syntaxhighlight lang="pli"> /* FIND THE SMALLEST NUMBER > THE PREVIOUS ONE WITH EXACTLY N DIVISORS */ |
|||
sequence_100H: procedure options (main); |
|||
/* PROGRAM-SPECIFIC %REPLACE STATEMENTS MUST APPEAR BEFORE THE %INCLUDE AS */ |
|||
/* E.G. THE CP/M PL/I COMPILER DOESN'T LIKE THEM TO FOLLOW PROCEDURES */ |
|||
/* PL/I */ |
|||
%replace maxnumber by 15; |
|||
/* PL/M */ /* |
|||
DECLARE MAXNUMBER LITERALLY '15'; |
|||
/* */ |
|||
/* PL/I DEFINITIONS */ |
|||
%include 'pg.inc'; |
|||
/* PL/M DEFINITIONS: CP/M BDOS SYSTEM CALL AND CONSOLE I/O ROUTINES, ETC. */ /* |
|||
DECLARE BINARY LITERALLY 'ADDRESS', CHARACTER LITERALLY 'BYTE'; |
|||
DECLARE FIXED LITERALLY ' ', BIT LITERALLY 'BYTE'; |
|||
DECLARE STATIC LITERALLY ' ', RETURNS LITERALLY ' '; |
|||
DECLARE FALSE LITERALLY '0', TRUE LITERALLY '1'; |
|||
DECLARE HBOUND LITERALLY 'LAST', SADDR LITERALLY '.'; |
|||
BDOSF: PROCEDURE( FN, ARG )BYTE; |
|||
DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END; |
|||
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END; |
|||
PRCHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END; |
|||
PRSTRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END; |
|||
PRNL: PROCEDURE; CALL PRCHAR( 0DH ); CALL PRCHAR( 0AH ); END; |
|||
PRNUMBER: PROCEDURE( N ); |
|||
DECLARE N ADDRESS; |
|||
DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE; |
|||
N$STR( W := LAST( N$STR ) ) = '$'; |
|||
N$STR( W := W - 1 ) = '0' + ( ( V := N ) MOD 10 ); |
|||
DO WHILE( ( V := V / 10 ) > 0 ); |
|||
N$STR( W := W - 1 ) = '0' + ( V MOD 10 ); |
|||
END; |
|||
CALL BDOS( 9, .N$STR( W ) ); |
|||
END PRNUMBER; |
|||
MODF: PROCEDURE( A, B )ADDRESS; |
|||
DECLARE ( A, B )ADDRESS; |
|||
RETURN( A MOD B ); |
|||
END MODF; |
|||
MIN: PROCEDURE( A, B ) ADDRESS; |
|||
DECLARE ( A, B ) ADDRESS; |
|||
IF A < B THEN RETURN( A ); ELSE RETURN( B ); |
|||
END MIN; |
|||
/* END LANGUAGE DEFINITIONS */ |
|||
/* TASK */ |
|||
COUNTDIVISORS: PROCEDURE( N )RETURNS /* THE DIVISOR COUNT OF N */ ( |
|||
FIXED BINARY ) |
|||
; |
|||
DECLARE N FIXED BINARY; |
|||
DECLARE ( I, I2, COUNT ) FIXED BINARY; |
|||
COUNT = 0; |
|||
I = 1; |
|||
I2 = 1; |
|||
DO WHILE( I2 < N ); |
|||
IF MODF( N, I ) = 0 THEN COUNT = COUNT + 2; |
|||
I = I + 1; |
|||
I2 = I * I; |
|||
END; |
|||
IF I2 = N THEN RETURN ( COUNT + 1 ); ELSE RETURN ( COUNT ); |
|||
END COUNTDIVISORS ; |
|||
DECLARE ( I, NEXT ) FIXED BINARY; |
|||
CALL PRSTRING( SADDR( 'THE FIRST $' ) ); |
|||
CALL PRNUMBER( MAXNUMBER ); |
|||
CALL PRSTRING( SADDR( ' TERMS OF THE SEQUENCE ARE:$' ) ); |
|||
NEXT = 1; |
|||
I = 1; |
|||
DO WHILE( NEXT <= MAXNUMBER ); |
|||
IF NEXT = COUNTDIVISORS( I ) THEN DO; |
|||
CALL PRCHAR( ' ' ); |
|||
CALL PRNUMBER( I ); |
|||
NEXT = NEXT + 1; |
|||
END; |
|||
I = I + 1; |
|||
END; |
|||
EOF: end sequence_100H;</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
THE FIRST 15 TERMS OF THE SEQUENCE ARE: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
|||
</pre> |
</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
<syntaxhighlight lang="python"> |
|||
<lang Python> |
|||
def divisors(n): |
def divisors(n): |
||
divs = [1] |
divs = [1] |
||
Line 808: | Line 1,581: | ||
for item in sequence(15): |
for item in sequence(15): |
||
print(item) |
print(item) |
||
</syntaxhighlight> |
|||
</lang> |
|||
<b>Output:</b> |
<b>Output:</b> |
||
<syntaxhighlight lang="python"> |
|||
<lang Python> |
|||
1 |
1 |
||
2 |
2 |
||
Line 826: | Line 1,599: | ||
4288 |
4288 |
||
4624 |
4624 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Quackery}}== |
|||
<code>factors</code> is defined at [[Factors of an integer#Quackery]]. |
|||
<syntaxhighlight lang="quackery"> [ stack ] is terms ( --> s ) |
|||
[ temp put |
|||
[] terms put |
|||
0 1 |
|||
[ dip 1+ |
|||
over factors size |
|||
over = if |
|||
[ over |
|||
terms take |
|||
swap join |
|||
terms put |
|||
1+ ] |
|||
terms share size |
|||
temp share = until ] |
|||
terms take |
|||
temp release |
|||
dip 2drop ] is task ( n --> [ ) |
|||
15 task echo</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[ 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 ]</pre> |
|||
=={{header|R}}== |
|||
<syntaxhighlight lang="rsplus">#Need to add 1 to account for skipping n. Not the most efficient way to count divisors, but quite clear. |
|||
divisorCount <- function(n) length(Filter(function(x) n %% x == 0, seq_len(n %/% 2))) + 1 |
|||
A06954 <- function(terms) |
|||
{ |
|||
out <- 1 |
|||
while((resultCount <- length(out)) != terms) |
|||
{ |
|||
n <- resultCount + 1 |
|||
out[n] <- out[resultCount] |
|||
while(divisorCount(out[n]) != n) out[n] <- out[n] + 1 |
|||
} |
|||
out |
|||
} |
|||
print(A06954(15))</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[1] 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 832: | Line 1,652: | ||
{{works with|Rakudo|2019.03}} |
{{works with|Rakudo|2019.03}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub div-count (\x) { |
||
return 2 if x.is-prime; |
return 2 if x.is-prime; |
||
+flat (1 .. x.sqrt.floor).map: -> \d { |
+flat (1 .. x.sqrt.floor).map: -> \d { |
||
Line 844: | Line 1,664: | ||
put "First $limit terms of OEIS:A069654"; |
put "First $limit terms of OEIS:A069654"; |
||
put (1..$limit).map: -> $n { my $ = $m = first { $n == .&div-count }, $m..Inf }; |
put (1..$limit).map: -> $n { my $ = $m = first { $n == .&div-count }, $m..Inf }; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 857: | Line 1,677: | ||
Optimization was included when examining ''even'' or ''odd'' index numbers (determine how much to increment the '''do''' loop). |
Optimization was included when examining ''even'' or ''odd'' index numbers (determine how much to increment the '''do''' loop). |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds and displays N numbers of the "anti─primes plus" sequence. */ |
||
parse arg N . /*obtain optional argument from the CL.*/ |
parse arg N . /*obtain optional argument from the CL.*/ |
||
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/ |
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/ |
||
Line 890: | Line 1,710: | ||
else if k*k>x then leave /*only divide up to the √ x */ |
else if k*k>x then leave /*only divide up to the √ x */ |
||
end /*k*/ /* [↑] this form of DO loop is faster.*/ |
end /*k*/ /* [↑] this form of DO loop is faster.*/ |
||
return # + 1 /*bump "proper divisors" to "divisors".*/</ |
return # + 1 /*bump "proper divisors" to "divisors".*/</syntaxhighlight> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |
||
Line 912: | Line 1,732: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : ANti-primes |
# Project : ANti-primes |
||
Line 950: | Line 1,770: | ||
next |
next |
||
return ansum |
return ansum |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 962: | Line 1,782: | ||
done... |
done... |
||
</pre> |
</pre> |
||
=={{header|RPL}}== |
|||
{{works with|HP|49g}} |
|||
≪ {1} |
|||
2 ROT '''FOR''' j |
|||
DUP DUP SIZE GET |
|||
'''DO''' 1 + '''UNTIL''' DUP DIVIS SIZE j == '''END''' |
|||
+ |
|||
'''NEXT ''' |
|||
≫ '<span style="color:blue">TASK</span>' STO |
|||
15 <span style="color:blue">TASK</span> |
|||
{{out}} |
|||
<pre> |
|||
1: {1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624} |
|||
</pre> |
|||
Runs in 10 minutes 55 on a HP-50g. |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">require 'prime' |
||
def num_divisors(n) |
def num_divisors(n) |
||
Line 981: | Line 1,818: | ||
p seq.take(15) |
p seq.take(15) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>[1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624] |
<pre>[1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624] |
||
Line 987: | Line 1,824: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func n_divisors(n, from=1) { |
||
from..Inf -> first_by { .sigma0 == n } |
from..Inf -> first_by { .sigma0 == n } |
||
} |
} |
||
Line 993: | Line 1,830: | ||
with (1) { |from| |
with (1) { |from| |
||
say 15.of { from = n_divisors(_+1, from) } |
say 15.of { from = n_divisors(_+1, from) } |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
[1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624] |
[1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624] |
||
</pre> |
|||
=={{header|Swift}}== |
|||
Includes an optimization borrowed from the Pascal example above. |
|||
<syntaxhighlight lang="swift">// See https://en.wikipedia.org/wiki/Divisor_function |
|||
func divisorCount(number: Int) -> Int { |
|||
var n = number |
|||
var total = 1 |
|||
// Deal with powers of 2 first |
|||
while n % 2 == 0 { |
|||
total += 1 |
|||
n /= 2 |
|||
} |
|||
// Odd prime factors up to the square root |
|||
var p = 3 |
|||
while p * p <= n { |
|||
var count = 1 |
|||
while n % p == 0 { |
|||
count += 1 |
|||
n /= p |
|||
} |
|||
total *= count |
|||
p += 2 |
|||
} |
|||
// If n > 1 then it's prime |
|||
if n > 1 { |
|||
total *= 2 |
|||
} |
|||
return total |
|||
} |
|||
let limit = 32 |
|||
var n = 1 |
|||
var next = 1 |
|||
while next <= limit { |
|||
if next == divisorCount(number: n) { |
|||
print(n, terminator: " ") |
|||
next += 1 |
|||
if next > 4 && divisorCount(number: next) == 2 { |
|||
n = 1 << (next - 1) - 1; |
|||
} |
|||
} |
|||
n += 1 |
|||
} |
|||
print()</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 4632 65536 65572 262144 262192 263169 269312 4194304 4194306 4477456 4493312 4498641 4498752 268435456 268437200 1073741824 1073741830 |
|||
</pre> |
</pre> |
||
Line 1,002: | Line 1,888: | ||
{{trans|Phix}} |
{{trans|Phix}} |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
< |
<syntaxhighlight lang="wren">import "./math" for Int |
||
var limit = 24 |
var limit = 24 |
||
Line 1,018: | Line 1,904: | ||
} |
} |
||
System.print("The first %(limit) terms are:") |
System.print("The first %(limit) terms are:") |
||
System.print(res)</ |
System.print(res)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,024: | Line 1,910: | ||
The first 24 terms are: |
The first 24 terms are: |
||
[1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624, 4632, 65536, 65572, 262144, 262192, 263169, 269312, 4194304, 4194306] |
[1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624, 4632, 65536, 65572, 262144, 262192, 263169, 269312, 4194304, 4194306] |
||
</pre> |
|||
=={{header|XPL0}}== |
|||
<syntaxhighlight lang="xpl0">func Divs(N); \Count divisors of N |
|||
int N, D, C; |
|||
[C:= 0; |
|||
if N > 1 then |
|||
[D:= 1; |
|||
repeat if rem(N/D) = 0 then C:= C+1; |
|||
D:= D+1; |
|||
until D >= N; |
|||
]; |
|||
return C; |
|||
]; |
|||
int An, N; |
|||
[An:= 1; N:= 0; |
|||
loop [if Divs(An) = N then |
|||
[IntOut(0, An); ChOut(0, ^ ); |
|||
N:= N+1; |
|||
if N >= 15 then quit; |
|||
]; |
|||
An:= An+1; |
|||
]; |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 |
|||
</pre> |
</pre> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn countDivisors(n) |
||
{ [1..(n).toFloat().sqrt()] .reduce('wrap(s,i){ s + (if(0==n%i) 1 + (i!=n/i)) },0) }</ |
{ [1..(n).toFloat().sqrt()] .reduce('wrap(s,i){ s + (if(0==n%i) 1 + (i!=n/i)) },0) }</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">n:=15; |
||
println("The first %d anti-primes plus are:".fmt(n)); |
println("The first %d anti-primes plus are:".fmt(n)); |
||
(1).walker(*).tweak( |
(1).walker(*).tweak( |
||
fcn(n,rn){ if(rn.value==countDivisors(n)){ rn.inc(); n } else Void.Skip }.fp1(Ref(1))) |
fcn(n,rn){ if(rn.value==countDivisors(n)){ rn.inc(); n } else Void.Skip }.fp1(Ref(1))) |
||
.walk(n).concat(" ").println();</ |
.walk(n).concat(" ").println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
Latest revision as of 11:38, 4 February 2024
You are encouraged to solve this task according to the task description, using any language you may know.
Calculate the sequence where each term an is the smallest natural number greater than the previous term, that has exactly n divisors.
- Task
Show here, on this page, at least the first 15 terms of the sequence.
- See also
- Related tasks
11l
F divisors(n)
V divs = [1]
L(ii) 2 .< Int(n ^ 0.5) + 3
I n % ii == 0
divs.append(ii)
divs.append(Int(n / ii))
divs.append(n)
R Array(Set(divs))
F sequence(max_n)
V previous = 0
V n = 0
[Int] r
L
n++
V ii = previous
I n > max_n
L.break
L
ii++
I divisors(ii).len == n
r.append(ii)
previous = ii
L.break
R r
L(item) sequence(15)
print(item)
- Output:
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Action!
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
CARD FUNC CountDivisors(CARD a)
CARD i,count
i=1 count=0
WHILE i*i<=a
DO
IF a MOD i=0 THEN
IF i=a/i THEN
count==+1
ELSE
count==+2
FI
FI
i==+1
OD
RETURN (count)
PROC Main()
CARD a
BYTE i
a=1
FOR i=1 TO 15
DO
WHILE CountDivisors(a)#i
DO
a==+1
OD
IF i>1 THEN
Print(", ")
FI
PrintC(a)
OD
RETURN
- Output:
Screenshot from Atari 8-bit computer
1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624
Ada
with Ada.Text_IO;
procedure Show_Sequence is
function Count_Divisors (N : in Natural) return Natural is
Count : Natural := 0;
I : Natural;
begin
I := 1;
while I**2 <= N loop
if N mod I = 0 then
if I = N / I then
Count := Count + 1;
else
Count := Count + 2;
end if;
end if;
I := I + 1;
end loop;
return Count;
end Count_Divisors;
procedure Show (Max : in Natural) is
use Ada.Text_IO;
N : Natural := 1;
Begin
Put_Line ("The first" & Max'Image & "terms of the sequence are:");
for Divisors in 1 .. Max loop
while Count_Divisors (N) /= Divisors loop
N := N + 1;
end loop;
Put (N'Image);
end loop;
New_Line;
end Show;
begin
Show (15);
end Show_Sequence;
- Output:
The first 15terms of the sequence are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
ALGOL 68
with a small optimisation.
BEGIN
PROC count divisors = ( INT n )INT:
BEGIN
INT i2, count := 0;
FOR i WHILE ( i2 := i * i ) < n DO
IF n MOD i = 0 THEN count +:= 2 FI
OD;
IF i2 = n THEN count + 1 ELSE count FI
END # count divisors # ;
INT max = 15;
print( ( "The first ", whole( max, 0 ), " terms of the sequence are:" ) );
INT next := 1;
FOR i WHILE next <= max DO
IF next = count divisors( i ) THEN
print( ( " ", whole( i, 0 ) ) );
next +:= 1
FI
OD
END
- Output:
The first 15 terms of the sequence are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
ALGOL W
...via Algol 68 and with a small optimisation.
begin
integer max, next, i;
integer procedure countDivisors ( Integer value n ) ;
begin
integer count, i, i2;
count := 0;
i := 1;
while begin i2 := i * i;
i2 < n
end do begin
if n rem i = 0 then count := count + 2;
i := i + 1
end;
if i2 = n then count + 1 else count
end countDivisors ;
max := 15;
write( i_w := 1, s_w := 0, "The first ", max, " terms of the sequence are: " );
i := next := 1;
while next <= max do begin
if next = countDivisors( i ) then begin
writeon( i_w := 1, s_w := 0, " ", i );
next := next + 1
end;
i := i + 1
end;
write()
end.
- Output:
The first 15 terms of the sequence are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Arturo
i: new 0
next: new 1
MAX: 15
while [next =< MAX][
if next = size factors i [
prints ~"|i| "
inc 'next
]
inc 'i
]
print ""
- Output:
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
AutoHotkey
MAX := 15
next := 1, i := 1
while (next <= MAX)
if (next = countDivisors(A_Index))
Res.= A_Index ", ", next++
MsgBox % "The first " MAX " terms of the sequence are:`n" Trim(res, ", ")
return
countDivisors(n){
while (A_Index**2 <= n)
if !Mod(n, A_Index)
count += (A_Index = n/A_Index) ? 1 : 2
return count
}
Outputs:
The first 15 terms of the sequence are: 1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624
AWK
# syntax: GAWK -f SEQUENCE_SMALLEST_NUMBER_GREATER_THAN_PREVIOUS_TERM_WITH_EXACTLY_N_DIVISORS.AWK
# converted from Kotlin
BEGIN {
limit = 15
printf("first %d terms:",limit)
n = 1
while (n <= limit) {
if (n == count_divisors(++i)) {
printf(" %d",i)
n++
}
}
printf("\n")
exit(0)
}
function count_divisors(n, count,i) {
for (i=1; i*i<=n; i++) {
if (n % i == 0) {
count += (i == n / i) ? 1 : 2
}
}
return(count)
}
- Output:
first 15 terms: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
BASIC
BASIC256
UPTO = 15
i = 2
nfound = 1
print 1; " "; #special case
while nfound < UPTO
n = divisors(i)
if n = nfound + 1 then
nfound += 1
print i; " ";
end if
i += 1
end while
end
function divisors(n)
#find the number of divisors of an integer
r = 2
for i = 2 to n\2
if n mod i = 0 then r += 1
next i
return r
end function
Gambas
Public Sub Main()
Dim UPTO As Integer = 15, i As Integer = 2
Dim n As Integer, nfound As Integer = 1
Print 1; " "; 'special case
While nfound < UPTO
n = divisors(i)
If n = nfound + 1 Then
nfound += 1
Print i; " ";
End If
i += 1
Wend
End Sub
Function divisors(n As Integer) As Integer
'find the number of divisors of an integer
Dim r As Integer = 2, i As Integer
For i = 2 To n \ 2
If n Mod i = 0 Then r += 1
Next
Return r
End Function
PureBasic
Procedure.i divisors(n)
;find the number of divisors of an integer
Define.i r, i
r = 2
For i = 2 To n/2
If n % i = 0: r + 1
EndIf
Next i
ProcedureReturn r
EndProcedure
OpenConsole()
Define.i UPTO, i, n, found
UPTO = 15
i = 2
nfound = 1
Print("1 ") ;special case
While nfound < UPTO
n = divisors(i)
If n = nfound + 1:
nfound + 1
Print(Str(i) + " ")
EndIf
i + 1
Wend
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
QBasic
FUNCTION divisors (n)
'find the number of divisors of an integer
r = 2
FOR i = 2 TO n \ 2
IF n MOD i = 0 THEN r = r + 1
NEXT i
divisors = r
END FUNCTION
UPTO = 15
i = 2
nfound = 1
PRINT 1; 'special case
WHILE nfound < UPTO
n = divisors(i)
IF n = nfound + 1 THEN
nfound = nfound + 1
PRINT i;
END IF
i = i + 1
WEND
Run BASIC
UPTO = 15
i = 2
nfound = 1
print 1; " "; 'special case
while nfound < UPTO
n = divisors(i)
if n = nfound + 1 then
nfound = nfound + 1
print i; " ";
end if
i = i + 1
wend
print
end
function divisors(n)
'find the number of divisors of an integer
r = 2
for i = 2 to n / 2
if n mod i = 0 then r = r + 1
next i
divisors = r
end function
XBasic
PROGRAM "program name"
VERSION "0.0000"
DECLARE FUNCTION Entry ()
DECLARE FUNCTION divisors (n)
FUNCTION Entry ()
UPTO = 15
i = 2
nfound = 1
PRINT 1; 'special case
DO WHILE nfound < UPTO
n = divisors(i)
IF n = nfound + 1 THEN
INC nfound
PRINT i;
END IF
INC i
LOOP
END FUNCTION
FUNCTION divisors (n)
'find the number of divisors of an integer
r = 2
FOR i = 2 TO n / 2
IF n MOD i = 0 THEN INC r
NEXT i
RETURN r
END FUNCTION
END PROGRAM
Yabasic
UPTO = 15
i = 2
nfound = 1
print 1, " "; //special case
while nfound < UPTO
n = divisors(i)
if n = nfound + 1 then
nfound = nfound + 1
print i, " ";
fi
i = i + 1
end while
print
end
sub divisors(n)
local r, i
//find the number of divisors of an integer
r = 2
for i = 2 to n / 2
if mod(n, i) = 0 r = r + 1
next i
return r
end sub
C
#include <stdio.h>
#define MAX 15
int count_divisors(int n) {
int i, count = 0;
for (i = 1; i * i <= n; ++i) {
if (!(n % i)) {
if (i == n / i)
count++;
else
count += 2;
}
}
return count;
}
int main() {
int i, next = 1;
printf("The first %d terms of the sequence are:\n", MAX);
for (i = 1; next <= MAX; ++i) {
if (next == count_divisors(i)) {
printf("%d ", i);
next++;
}
}
printf("\n");
return 0;
}
- Output:
The first 15 terms of the sequence are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
C++
#include <iostream>
#define MAX 15
using namespace std;
int count_divisors(int n) {
int count = 0;
for (int i = 1; i * i <= n; ++i) {
if (!(n % i)) {
if (i == n / i)
count++;
else
count += 2;
}
}
return count;
}
int main() {
cout << "The first " << MAX << " terms of the sequence are:" << endl;
for (int i = 1, next = 1; next <= MAX; ++i) {
if (next == count_divisors(i)) {
cout << i << " ";
next++;
}
}
cout << endl;
return 0;
}
- Output:
The first 15 terms of the sequence are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Alternative
More terms and quicker than the straightforward C version above. Try It Online!
#include <cstdio>
#include <chrono>
using namespace std::chrono;
const int MAX = 32;
unsigned int getDividersCnt(unsigned int n) {
unsigned int d = 3, q, dRes, res = 1;
while (!(n & 1)) { n >>= 1; res++; }
while ((d * d) <= n) { q = n / d; if (n % d == 0) { dRes = 0;
do { dRes += res; n = q; q /= d; } while (n % d == 0);
res += dRes; } d += 2; } return n != 1 ? res << 1 : res; }
int main() { unsigned int i, nxt, DivCnt;
printf("The first %d anti-primes plus are: ", MAX);
auto st = steady_clock::now(); i = nxt = 1; do {
if ((DivCnt = getDividersCnt(i)) == nxt ) { printf("%d ", i);
if ((++nxt > 4) && (getDividersCnt(nxt) == 2))
i = (1 << (nxt - 1)) - 1; } i++; } while (nxt <= MAX);
printf("%d ms", (int)(duration<double>(steady_clock::now() - st).count() * 1000));
}
- Output:
The first 32 anti-primes plus are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 4632 65536 65572 262144 262192 263169 269312 4194304 4194306 4477456 4493312 4498641 4498752 268435456 268437200 1073741824 1073741830 223 ms
Delphi
{These routines would normally be in a library, but the shown here for clarity}
function GetAllProperDivisors(N: Integer;var IA: TIntegerDynArray): integer;
{Make a list of all the "proper dividers" for N}
{Proper dividers are the of numbers the divide evenly into N}
var I: integer;
begin
SetLength(IA,0);
for I:=1 to N-1 do
if (N mod I)=0 then
begin
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=I;
end;
Result:=Length(IA);
end;
function GetAllDivisors(N: Integer;var IA: TIntegerDynArray): integer;
{Make a list of all the "proper dividers" for N, Plus N itself}
begin
Result:=GetAllProperDivisors(N,IA)+1;
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=N;
end;
procedure SmallestWithNDivisors(Memo: TMemo);
var N,Count: integer;
var IA: TIntegerDynArray;
begin
Count:=1;
for N:=1 to high(Integer) do
if Count=GetAllDivisors(N,IA) then
begin
Memo.Lines.Add(IntToStr(Count)+' - '+IntToStr(N));
Inc(Count);
if Count>15 then break;
end;
end;
- Output:
1 - 1 2 - 2 3 - 4 4 - 6 5 - 16 6 - 18 7 - 64 8 - 66 9 - 100 10 - 112 11 - 1024 12 - 1035 13 - 4096 14 - 4288 15 - 4624 Elapsed Time: 58.590 ms.
Dyalect
func countDivisors(n) {
var count = 0
var i = 1
while i * i <= n {
if n % i == 0 {
if i == n / i {
count += 1
} else {
count += 2
}
}
i += 1
}
return count
}
let max = 15
print("The first \(max) terms of the sequence are:")
var (i, next) = (1, 1)
while next <= max {
if next == countDivisors(i) {
print("\(i) ", terminator: "")
next += 1
}
i += 1
}
print()
- Output:
The first 15 terms of the sequence are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
EasyLang
func ndivs n .
i = 1
while i <= sqrt n
if n mod i = 0
cnt += 2
if i = n div i
cnt -= 1
.
.
i += 1
.
return cnt
.
n = 1
while n <= 15
i += 1
if n = ndivs i
write i & " "
n += 1
.
.
F#
First 28 are easy with a Naive implementation
// Nigel Galloway: November 19th., 2017
let fN g=[1..(float>>sqrt>>int)g]|>List.fold(fun Σ n->if g%n>0 then Σ else if g/n=n then Σ+1 else Σ+2) 0
let A069654=let rec fG n g=seq{match g-fN n with 0->yield n; yield! fG(n+1)(g+1) |_->yield! fG(n+1)g} in fG 1 1
A069654 |> Seq.take 28|>Seq.iter(printf "%d "); printfn ""
- Output:
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 4632 65536 65572 262144 262192 263169 269312 4194304 4194306 4477456 4493312 4498641 4498752
Factor
USING: io kernel math math.primes.factors prettyprint sequences ;
: next ( n num -- n' num' )
[ 2dup divisors length = ] [ 1 + ] do until [ 1 + ] dip ;
: A069654 ( n -- seq )
[ 2 1 ] dip [ [ next ] keep ] replicate 2nip ;
"The first 15 terms of the sequence are:" print 15 A069654 .
- Output:
The first 15 terms of the sequence are: { 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 }
FreeBASIC
#define UPTO 15
function divisors(byval n as ulongint) as uinteger
'find the number of divisors of an integer
dim as integer r = 2, i
for i = 2 to n\2
if n mod i = 0 then r += 1
next i
return r
end function
dim as ulongint i = 2
dim as integer n, nfound = 1
print 1;" "; 'special case
while nfound < UPTO
n = divisors(i)
if n = nfound + 1 then
nfound += 1
print i;" ";
end if
i+=1
wend
print
end
- Output:
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Go
package main
import "fmt"
func countDivisors(n int) int {
count := 0
for i := 1; i*i <= n; i++ {
if n%i == 0 {
if i == n/i {
count++
} else {
count += 2
}
}
}
return count
}
func main() {
const max = 15
fmt.Println("The first", max, "terms of the sequence are:")
for i, next := 1, 1; next <= max; i++ {
if next == countDivisors(i) {
fmt.Printf("%d ", i)
next++
}
}
fmt.Println()
}
- Output:
The first 15 terms of the sequence are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Haskell
import Text.Printf (printf)
sequence_A069654 :: [(Int,Int)]
sequence_A069654 = go 1 $ (,) <*> countDivisors <$> [1..]
where go t ((n,c):xs) | c == t = (t,n):go (succ t) xs
| otherwise = go t xs
countDivisors n = foldr f 0 [1..floor $ sqrt $ realToFrac n]
where f x r | n `mod` x == 0 = if n `div` x == x then r+1 else r+2
| otherwise = r
main :: IO ()
main = mapM_ (uncurry $ printf "a(%2d)=%5d\n") $ take 15 sequence_A069654
- Output:
a( 1)= 1 a( 2)= 2 a( 3)= 4 a( 4)= 6 a( 5)= 16 a( 6)= 18 a( 7)= 64 a( 8)= 66 a( 9)= 100 a(10)= 112 a(11)= 1024 a(12)= 1035 a(13)= 4096 a(14)= 4288 a(15)= 4624
J
sieve=: 3 :0
NB. sieve y returns a vector of y boxes.
NB. In each box is an array of 2 columns.
NB. The first column is the factor tally
NB. and the second column is a number with
NB. that many factors.
NB. Rather than factoring, the algorithm
NB. counts prime seive cell hits.
NB. The boxes are not ordered by factor tally.
range=. <. + i.@:|@:-
tally=. y#0
for_i.#\tally do.
j=. }:^:(y<:{:)i * 1 range >: <. y % i
tally=. j >:@:{`[`]} tally
end.
(</.~ {."1) (,. i.@:#)tally
)
A=: sieve 100000 B=: /:~ A missing=: [: (-.~i.@:#) (<0 0)&{&> C=: ({.~ {.@:missing) B D=:{:"1 L:_1 C (] , [ {~ (I. {:))&.>/@:|. D +-----------------------------------------------------------------------+ |0 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 4632 65536 65572| +-----------------------------------------------------------------------+
Intermediate steps and explanation for a smaller sieve:
] A=: sieve 60 +---+---+----+----+----+----+----+----+----+-----+ |0 0|1 1|2 2|3 4|4 6|6 12|5 16|8 24|9 36|10 48| | | |2 3|3 9|4 8|6 18| |8 30| | | | | |2 5|3 25|4 10|6 20| |8 40| | | | | |2 7|3 49|4 14|6 28| |8 42| | | | | |2 11| |4 15|6 32| |8 54| | | | | |2 13| |4 21|6 44| |8 56| | | | | |2 17| |4 22|6 45| | | | | | | |2 19| |4 26|6 50| | | | | | | |2 23| |4 27|6 52| | | | | | | |2 29| |4 33| | | | | | | | |2 31| |4 34| | | | | | | | |2 37| |4 35| | | | | | | | |2 41| |4 38| | | | | | | | |2 43| |4 39| | | | | | | | |2 47| |4 46| | | | | | | | |2 53| |4 51| | | | | | | | |2 59| |4 55| | | | | | | | | | |4 57| | | | | | | | | | |4 58| | | | | | +---+---+----+----+----+----+----+----+----+-----+ ] B=: /:~ A NB. ascending sort by tally +---+---+----+----+----+----+----+----+----+-----+ |0 0|1 1|2 2|3 4|4 6|5 16|6 12|8 24|9 36|10 48| | | |2 3|3 9|4 8| |6 18|8 30| | | | | |2 5|3 25|4 10| |6 20|8 40| | | | | |2 7|3 49|4 14| |6 28|8 42| | | | | |2 11| |4 15| |6 32|8 54| | | | | |2 13| |4 21| |6 44|8 56| | | | | |2 17| |4 22| |6 45| | | | | | |2 19| |4 26| |6 50| | | | | | |2 23| |4 27| |6 52| | | | | | |2 29| |4 33| | | | | | | | |2 31| |4 34| | | | | | | | |2 37| |4 35| | | | | | | | |2 41| |4 38| | | | | | | | |2 43| |4 39| | | | | | | | |2 47| |4 46| | | | | | | | |2 53| |4 51| | | | | | | | |2 59| |4 55| | | | | | | | | | |4 57| | | | | | | | | | |4 58| | | | | | +---+---+----+----+----+----+----+----+----+-----+ NB. truncate to the fully populated length NB. missing lists the unavailable factor tallies missing=: [: (-.~i.@:#) (<0 0)&{&> ] C=: ({.~ {.@:missing) B NB. retain tally counts with no holes +---+---+----+----+----+----+----+ |0 0|1 1|2 2|3 4|4 6|5 16|6 12| | | |2 3|3 9|4 8| |6 18| | | |2 5|3 25|4 10| |6 20| | | |2 7|3 49|4 14| |6 28| | | |2 11| |4 15| |6 32| | | |2 13| |4 21| |6 44| | | |2 17| |4 22| |6 45| | | |2 19| |4 26| |6 50| | | |2 23| |4 27| |6 52| | | |2 29| |4 33| | | | | |2 31| |4 34| | | | | |2 37| |4 35| | | | | |2 41| |4 38| | | | | |2 43| |4 39| | | | | |2 47| |4 46| | | | | |2 53| |4 51| | | | | |2 59| |4 55| | | | | | | |4 57| | | | | | | |4 58| | | +---+---+----+----+----+----+----+ NB. Remove the tally column from each box (by retaining the values) ,.L:_1 D=:{:"1 L:_1 C +-+-+--+--+--+--+--+ |0|1| 2| 4| 6|16|12| | | | 3| 9| 8| |18| | | | 5|25|10| |20| | | | 7|49|14| |28| | | |11| |15| |32| | | |13| |21| |44| | | |17| |22| |45| | | |19| |26| |50| | | |23| |27| |52| | | |29| |33| | | | | |31| |34| | | | | |37| |35| | | | | |41| |38| | | | | |43| |39| | | | | |47| |46| | | | | |53| |51| | | | | |59| |55| | | | | | | |57| | | | | | | |58| | | +-+-+--+--+--+--+--+ NB. finally enlist successively larger values (] , [ {~ (I. {:))&.>/@:|. D +---------------+ |0 1 2 4 6 16 18| +---------------+
Java
public class AntiPrimesPlus {
static int count_divisors(int n) {
int count = 0;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
if (i == n / i)
count++;
else
count += 2;
}
}
return count;
}
public static void main(String[] args) {
final int max = 15;
System.out.printf("The first %d terms of the sequence are:\n", max);
for (int i = 1, next = 1; next <= max; ++i) {
if (next == count_divisors(i)) {
System.out.printf("%d ", i);
next++;
}
}
System.out.println();
}
}
- Output:
The first 15 terms of the sequence are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
jq
Adapted from Julia
Works with gojq, the Go implementation of jq
# The number of prime factors (as in prime factorization)
def numfactors:
. as $num
| reduce range(1; 1 + sqrt|floor) as $i (null;
if ($num % $i) == 0
then ($num / $i) as $r
| if $i == $r then .+1 else .+2 end
else .
end );
# Output: a stream
def A06954:
foreach range(1; infinite) as $i ({k: 0};
.j = .k + 1
| until( $i == (.j | numfactors); .j += 1)
| .k = .j ;
.j ) ;
"First 15 terms of OEIS sequence A069654: ",
[limit(15; A06954)]
- Output:
First 15 terms of OEIS sequence A069654: [1,2,4,6,16,18,64,66,100,112,1024,1035,4096,4288,4624]
Julia
using Primes
function numfactors(n)
f = [one(n)]
for (p,e) in factor(n)
f = reduce(vcat, [f*p^j for j in 1:e], init=f)
end
length(f)
end
function A06954(N)
println("First $N terms of OEIS sequence A069654: ")
k = 0
for i in 1:N
j = k
while (j += 1) > 0
if i == numfactors(j)
print("$j ")
k = j
break
end
end
end
end
A06954(15)
- Output:
First 15 terms of OEIS sequence A069654: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Kotlin
// Version 1.3.21
const val MAX = 15
fun countDivisors(n: Int): Int {
var count = 0
var i = 1
while (i * i <= n) {
if (n % i == 0) {
count += if (i == n / i) 1 else 2
}
i++
}
return count
}
fun main() {
println("The first $MAX terms of the sequence are:")
var i = 1
var next = 1
while (next <= MAX) {
if (next == countDivisors(i)) {
print("$i ")
next++
}
i++
}
println()
}
- Output:
The first 15 terms of the sequence are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Mathematica / Wolfram Language
res = {#, DivisorSigma[0, #]} & /@ Range[100000];
highest = 0;
filter = {};
Do[
If[r[[2]] == highest + 1,
AppendTo[filter, r[[1]]];
highest = r[[2]]
]
,
{r, res}
]
Take[filter, 15]
- Output:
{1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624}
Maxima
sngptend(n):=block([i:1,count:1,result:[]],
while count<=n do (if length(listify(divisors(i)))=count then (result:endcons(i,result),count:count+1),i:i+1),
result)$
/* Test case */
sngptend(15);
- Output:
[1,2,4,6,16,18,64,66,100,112,1024,1035,4096,4288,4624]
MiniScript
This GUI implementation is for use with Mini Micro.
divisors = function(n)
divs = {1: 1}
divs[n] = 1
i = 2
while i * i <= n
if n % i == 0 then
divs[i] = 1
divs[n / i] = 1
end if
i += 1
end while
return divs.indexes
end function
counts = []
j = 1
for i in range(1, 15)
while divisors(j).len != i
j += 1
end while
counts.push(j)
end for
print "The first 15 terms in the sequence are:"
print counts.join(", ")
- Output:
The first 15 terms in the sequence are: 1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624
Nim
import strformat
const MAX = 15
func countDivisors(n: int): int =
var i = 1
var count = 0
while i * i <= n:
if n mod i == 0:
if i == n div i:
inc count
else:
inc count, 2
inc i
count
var i, next = 1
echo fmt"The first {MAX} terms of the sequence are:"
while next <= MAX:
if next == countDivisors(i):
write(stdout, fmt"{i} ")
inc next
inc i
write(stdout, "\n")
- Output:
The first 15 terms of the sequence are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Pascal
Counting divisors by prime factorisation.
If divCnt= Count of divisors is prime then the only candidate ist n = prime^(divCnt-1).
There will be more rules. If divCnt is odd then the divisors of divCnt are a^(even_factor*i)*..*k^(even_factor*j).
I think of next = 33 aka 11*3 with the solution 1031^2 * 2^10=1,088,472,064 with a big distance to next= 32 => 1073741830.
Try it online!
program AntiPrimesPlus;
{$IFDEF FPC}
{$MODE Delphi}
{$ELSE}
{$APPTYPE CONSOLE} // delphi
{$ENDIF}
uses
sysutils,math;
const
MAX =32;
function getDividersCnt(n:Uint32):Uint32;
// getDividersCnt by dividing n into its prime factors
// aka n = 2250 = 2^1*3^2*5^3 has (1+1)*(2+1)*(3+1)= 24 dividers
var
divi,quot,deltaRes,rest : Uint32;
begin
result := 1;
//divi := 2; //separat without division
while Not(Odd(n)) do
Begin
n := n SHR 1;
inc(result);
end;
//from now on only odd numbers
divi := 3;
while (sqr(divi)<=n) do
Begin
DivMod(n,divi,quot,rest);
if rest = 0 then
Begin
deltaRes := 0;
repeat
inc(deltaRes,result);
n := quot;
DivMod(n,divi,quot,rest);
until rest <> 0;
inc(result,deltaRes);
end;
inc(divi,2);
end;
//if last factor of n is prime
IF n <> 1 then
result := result*2;
end;
var
T0 : Int64;
i,next,DivCnt: Uint32;
begin
writeln('The first ',MAX,' anti-primes plus are:');
T0:= GetTickCount64;
i := 1;
next := 1;
repeat
DivCnt := getDividersCnt(i);
IF DivCnt= next then
Begin
write(i,' ');
inc(next);
//if next is prime then only prime( => mostly 2 )^(next-1) is solution
IF (next > 4) AND (getDividersCnt(next) = 2) then
i := 1 shl (next-1) -1;// i is incremented afterwards
end;
inc(i);
until Next > MAX;
writeln;
writeln(GetTickCount64-T0,' ms');
end.
- Output:
The first 32 anti-primes plus are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 4632 65536 65572 262144 262192 263169 269312 4194304 4194306 4477456 4493312 4498641 4498752 268435456 268437200 1073741824 1073741830 525 ms
Perl
use strict;
use warnings;
use ntheory 'divisors';
print "First 15 terms of OEIS: A069654\n";
my $m = 0;
for my $n (1..15) {
my $l = $m;
while (++$l) {
print("$l "), $m = $l, last if $n == divisors($l);
}
}
- Output:
First 15 terms of OEIS: A069654 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Phix
Uses the optimisation trick from pascal, of n:=power(2,next-1) when next is a prime>4.
with javascript_semantics constant limit = 15 sequence res = repeat(0,limit) integer next = 1 atom n = 1 while next<=limit do integer k = length(factors(n,1)) if k=next then res[k] = n next += 1 if next>4 and is_prime(next) then n := power(2,next-1)-1 -- (-1 for +=1 next) end if end if n += 1 end while printf(1,"The first %d terms are: %v\n",{limit,res})
- Output:
The first 15 terms are: {1,2,4,6,16,18,64,66,100,112,1024,1035,4096,4288,4624}
You can raise the limit to 32, the 25th takes about 4s but the rest are all near-instant, however I lost patience waiting for the 33rd.
(The last two trailing .0 just mean "not a 31-bit integer" and don't appear when run on 64 bit.)
The first 32 terms are: {1,2,4,6,16,18,64,66,100,112,1024,1035,4096,4288,4624, 4632,65536,65572,262144,262192,263169,269312,4194304, 4194306,4477456,4493312,4498641,4498752,268435456, 268437200,1073741824.0,1073741830.0}
PL/I
PL/M
via Algol 68
100H: /* FIND THE SMALLEST NUMBER > THE PREVIOUS ONE WITH EXACTLY N DIVISORS */
/* CP/M BDOS SYSTEM CALL */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
/* CONSOLE OUTPUT ROUTINES */
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$STRING( .( 0DH, 0AH, '$' ) ); END;
PR$NUMBER: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE INITIAL( '.....$' ), W BYTE;
N$STR( W := LAST( N$STR ) - 1 ) = '0' + ( ( V := N ) MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
/* TASK */
/* RETURNS THE DIVISOR COUNT OF N */
COUNT$DIVISORS: PROCEDURE( N )ADDRESS;
DECLARE N ADDRESS;
DECLARE ( I, I2, COUNT ) ADDRESS;
COUNT = 0;
I = 1;
DO WHILE( ( I2 := I * I ) < N );
IF N MOD I = 0 THEN COUNT = COUNT + 2;
I = I + 1;
END;
IF I2 = N THEN RETURN ( COUNT + 1 ); ELSE RETURN ( COUNT );
END COUNT$DIVISORS ;
DECLARE MAX LITERALLY '15';
DECLARE ( I, NEXT ) ADDRESS;
CALL PR$STRING( .'THE FIRST $' );
CALL PR$NUMBER( MAX );
CALL PR$STRING( .' TERMS OF THE SEQUENCE ARE:$' );
NEXT = 1;
I = 1;
DO WHILE( NEXT <= MAX );
IF NEXT = COUNT$DIVISORS( I ) THEN DO;
CALL PR$CHAR( ' ' );
CALL PR$NUMBER( I );
NEXT = NEXT + 1;
END;
I = I + 1;
END;
EOF
- Output:
THE FIRST 15 TERMS OF THE SEQUENCE ARE: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
See also #Polyglot:PL/I and PL/M
Polyglot:PL/I and PL/M
... under CP/M (or an emulator)
Should work with many PL/I implementations.
The PL/I include file "pg.inc" can be found on the Polyglot:PL/I and PL/M page.
Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler.
/* FIND THE SMALLEST NUMBER > THE PREVIOUS ONE WITH EXACTLY N DIVISORS */
sequence_100H: procedure options (main);
/* PROGRAM-SPECIFIC %REPLACE STATEMENTS MUST APPEAR BEFORE THE %INCLUDE AS */
/* E.G. THE CP/M PL/I COMPILER DOESN'T LIKE THEM TO FOLLOW PROCEDURES */
/* PL/I */
%replace maxnumber by 15;
/* PL/M */ /*
DECLARE MAXNUMBER LITERALLY '15';
/* */
/* PL/I DEFINITIONS */
%include 'pg.inc';
/* PL/M DEFINITIONS: CP/M BDOS SYSTEM CALL AND CONSOLE I/O ROUTINES, ETC. */ /*
DECLARE BINARY LITERALLY 'ADDRESS', CHARACTER LITERALLY 'BYTE';
DECLARE FIXED LITERALLY ' ', BIT LITERALLY 'BYTE';
DECLARE STATIC LITERALLY ' ', RETURNS LITERALLY ' ';
DECLARE FALSE LITERALLY '0', TRUE LITERALLY '1';
DECLARE HBOUND LITERALLY 'LAST', SADDR LITERALLY '.';
BDOSF: PROCEDURE( FN, ARG )BYTE;
DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PRCHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PRSTRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PRNL: PROCEDURE; CALL PRCHAR( 0DH ); CALL PRCHAR( 0AH ); END;
PRNUMBER: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE;
N$STR( W := LAST( N$STR ) ) = '$';
N$STR( W := W - 1 ) = '0' + ( ( V := N ) MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL BDOS( 9, .N$STR( W ) );
END PRNUMBER;
MODF: PROCEDURE( A, B )ADDRESS;
DECLARE ( A, B )ADDRESS;
RETURN( A MOD B );
END MODF;
MIN: PROCEDURE( A, B ) ADDRESS;
DECLARE ( A, B ) ADDRESS;
IF A < B THEN RETURN( A ); ELSE RETURN( B );
END MIN;
/* END LANGUAGE DEFINITIONS */
/* TASK */
COUNTDIVISORS: PROCEDURE( N )RETURNS /* THE DIVISOR COUNT OF N */ (
FIXED BINARY )
;
DECLARE N FIXED BINARY;
DECLARE ( I, I2, COUNT ) FIXED BINARY;
COUNT = 0;
I = 1;
I2 = 1;
DO WHILE( I2 < N );
IF MODF( N, I ) = 0 THEN COUNT = COUNT + 2;
I = I + 1;
I2 = I * I;
END;
IF I2 = N THEN RETURN ( COUNT + 1 ); ELSE RETURN ( COUNT );
END COUNTDIVISORS ;
DECLARE ( I, NEXT ) FIXED BINARY;
CALL PRSTRING( SADDR( 'THE FIRST $' ) );
CALL PRNUMBER( MAXNUMBER );
CALL PRSTRING( SADDR( ' TERMS OF THE SEQUENCE ARE:$' ) );
NEXT = 1;
I = 1;
DO WHILE( NEXT <= MAXNUMBER );
IF NEXT = COUNTDIVISORS( I ) THEN DO;
CALL PRCHAR( ' ' );
CALL PRNUMBER( I );
NEXT = NEXT + 1;
END;
I = I + 1;
END;
EOF: end sequence_100H;
- Output:
THE FIRST 15 TERMS OF THE SEQUENCE ARE: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Python
def divisors(n):
divs = [1]
for ii in range(2, int(n ** 0.5) + 3):
if n % ii == 0:
divs.append(ii)
divs.append(int(n / ii))
divs.append(n)
return list(set(divs))
def sequence(max_n=None):
previous = 0
n = 0
while True:
n += 1
ii = previous
if max_n is not None:
if n > max_n:
break
while True:
ii += 1
if len(divisors(ii)) == n:
yield ii
previous = ii
break
if __name__ == '__main__':
for item in sequence(15):
print(item)
Output:
1
2
4
6
16
18
64
66
100
112
1024
1035
4096
4288
4624
Quackery
factors
is defined at Factors of an integer#Quackery.
[ stack ] is terms ( --> s )
[ temp put
[] terms put
0 1
[ dip 1+
over factors size
over = if
[ over
terms take
swap join
terms put
1+ ]
terms share size
temp share = until ]
terms take
temp release
dip 2drop ] is task ( n --> [ )
15 task echo
- Output:
[ 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 ]
R
#Need to add 1 to account for skipping n. Not the most efficient way to count divisors, but quite clear.
divisorCount <- function(n) length(Filter(function(x) n %% x == 0, seq_len(n %/% 2))) + 1
A06954 <- function(terms)
{
out <- 1
while((resultCount <- length(out)) != terms)
{
n <- resultCount + 1
out[n] <- out[resultCount]
while(divisorCount(out[n]) != n) out[n] <- out[n] + 1
}
out
}
print(A06954(15))
- Output:
[1] 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
Raku
(formerly Perl 6)
sub div-count (\x) {
return 2 if x.is-prime;
+flat (1 .. x.sqrt.floor).map: -> \d {
unless x % d { my \y = x div d; y == d ?? y !! (y, d) }
}
}
my $limit = 15;
my $m = 1;
put "First $limit terms of OEIS:A069654";
put (1..$limit).map: -> $n { my $ = $m = first { $n == .&div-count }, $m..Inf };
- Output:
First 15 terms of OEIS:A069654 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
REXX
Programming note: this Rosetta Code task (for 15 sequence numbers) doesn't require any optimization, but the code was optimized for listing higher numbers.
The method used is to find the number of proper divisors (up to the integer square root of X), and add one.
Optimization was included when examining even or odd index numbers (determine how much to increment the do loop).
/*REXX program finds and displays N numbers of the "anti─primes plus" sequence. */
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/
idx= 1 /*the maximum number of divisors so far*/
say '──index── ──anti─prime plus──' /*display a title for the numbers shown*/
#= 0 /*the count of anti─primes found " " */
do i=1 until #==N /*step through possible numbers by twos*/
d= #divs(i); if d\==idx then iterate /*get # divisors; Is too small? Skip.*/
#= # + 1; idx= idx + 1 /*found an anti─prime #; set new minD.*/
say center(#, 8) right(i, 15) /*display the index and the anti─prime.*/
end /*i*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
#divs: procedure; parse arg x 1 y /*X and Y: both set from 1st argument.*/
if x<7 then do /*handle special cases for numbers < 7.*/
if x<3 then return x /* " " " " one and two.*/
if x<5 then return x - 1 /* " " " " three & four*/
if x==5 then return 2 /* " " " " five. */
if x==6 then return 4 /* " " " " six. */
end
odd= x // 2 /*check if X is odd or not. */
if odd then #= 1; /*Odd? Assume Pdivisors count of 1.*/
else do; #= 3; y= x % 2 /*Even? " " " " 3.*/
end /* [↑] Even, so add 2 known divisors.*/
/* [↓] start with known number of Pdivs*/
do k=3 for x%2-3 by 1+odd while k<y /*for odd numbers, skip over the evens.*/
if x//k==0 then do /*if no remainder, then found a divisor*/
#= # + 2; y= x % k /*bump the # Pdivs; calculate limit Y.*/
if k>=y then do; #= # - 1; leave
end /* [↑] has the limit been reached? */
end /* ___ */
else if k*k>x then leave /*only divide up to the √ x */
end /*k*/ /* [↑] this form of DO loop is faster.*/
return # + 1 /*bump "proper divisors" to "divisors".*/
- output when using the default input:
──index── ──anti─prime plus── 1 1 2 2 3 4 4 6 5 16 6 18 7 64 8 66 9 100 10 112 11 1024 12 1035 13 4096 14 4288 15 4624
Ring
# Project : ANti-primes
see "working..." + nl
see "wait for done..." + nl + nl
see "the first 15 Anti-primes Plus are:" + nl + nl
num = 1
n = 0
result = list(15)
while num < 16
n = n + 1
div = factors(n)
if div = num
result[num] = n
num = num + 1
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 15 Anti-primes Plus are: [1,2,4,6,16,18,64,66,100,112,1024,1035,4096,4288,4624] done...
RPL
≪ {1}
2 ROT FOR j
DUP DUP SIZE GET
DO 1 + UNTIL DUP DIVIS SIZE j == END
+
NEXT
≫ 'TASK' STO
15 TASK
- Output:
1: {1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624}
Runs in 10 minutes 55 on a HP-50g.
Ruby
require 'prime'
def num_divisors(n)
n.prime_division.inject(1){|prod, (_p,n)| prod *= (n + 1) }
end
seq = Enumerator.new do |y|
cur = 0
(1..).each do |i|
if num_divisors(i) == cur + 1 then
y << i
cur += 1
end
end
end
p seq.take(15)
- Output:
[1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624]
Sidef
func n_divisors(n, from=1) {
from..Inf -> first_by { .sigma0 == n }
}
with (1) { |from|
say 15.of { from = n_divisors(_+1, from) }
}
- Output:
[1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624]
Swift
Includes an optimization borrowed from the Pascal example above.
// See https://en.wikipedia.org/wiki/Divisor_function
func divisorCount(number: Int) -> Int {
var n = number
var total = 1
// Deal with powers of 2 first
while n % 2 == 0 {
total += 1
n /= 2
}
// Odd prime factors up to the square root
var p = 3
while p * p <= n {
var count = 1
while n % p == 0 {
count += 1
n /= p
}
total *= count
p += 2
}
// If n > 1 then it's prime
if n > 1 {
total *= 2
}
return total
}
let limit = 32
var n = 1
var next = 1
while next <= limit {
if next == divisorCount(number: n) {
print(n, terminator: " ")
next += 1
if next > 4 && divisorCount(number: next) == 2 {
n = 1 << (next - 1) - 1;
}
}
n += 1
}
print()
- Output:
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624 4632 65536 65572 262144 262192 263169 269312 4194304 4194306 4477456 4493312 4498641 4498752 268435456 268437200 1073741824 1073741830
Wren
import "./math" for Int
var limit = 24
var res = List.filled(limit, 0)
var next = 1
var n = 1
while (next <= limit) {
var k = Int.divisors(n).count
if (k == next) {
res[k-1] = n
next = next + 1
if (next > 4 && Int.isPrime(next)) n = 2.pow(next-1) - 1
}
n = n + 1
}
System.print("The first %(limit) terms are:")
System.print(res)
- Output:
The first 24 terms are: [1, 2, 4, 6, 16, 18, 64, 66, 100, 112, 1024, 1035, 4096, 4288, 4624, 4632, 65536, 65572, 262144, 262192, 263169, 269312, 4194304, 4194306]
XPL0
func Divs(N); \Count divisors of N
int N, D, C;
[C:= 0;
if N > 1 then
[D:= 1;
repeat if rem(N/D) = 0 then C:= C+1;
D:= D+1;
until D >= N;
];
return C;
];
int An, N;
[An:= 1; N:= 0;
loop [if Divs(An) = N then
[IntOut(0, An); ChOut(0, ^ );
N:= N+1;
if N >= 15 then quit;
];
An:= An+1;
];
]
- Output:
1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
zkl
fcn countDivisors(n)
{ [1..(n).toFloat().sqrt()] .reduce('wrap(s,i){ s + (if(0==n%i) 1 + (i!=n/i)) },0) }
n:=15;
println("The first %d anti-primes plus are:".fmt(n));
(1).walker(*).tweak(
fcn(n,rn){ if(rn.value==countDivisors(n)){ rn.inc(); n } else Void.Skip }.fp1(Ref(1)))
.walk(n).concat(" ").println();
- Output:
The first 15 anti-primes plus are: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
- Programming Tasks
- Solutions by Programming Task
- 11l
- Action!
- Ada
- ALGOL 68
- ALGOL W
- Arturo
- AutoHotkey
- AWK
- BASIC
- BASIC256
- Gambas
- PureBasic
- QBasic
- Run BASIC
- XBasic
- Yabasic
- C
- C++
- Delphi
- SysUtils,StdCtrls
- Dyalect
- EasyLang
- F Sharp
- Factor
- FreeBASIC
- Go
- Haskell
- J
- Java
- Jq
- Julia
- Kotlin
- Mathematica
- Wolfram Language
- Maxima
- MiniScript
- Nim
- Pascal
- Perl
- Ntheory
- Phix
- PL/I
- PL/M
- Polyglot:PL/I and PL/M
- Python
- Quackery
- R
- Raku
- REXX
- Ring
- RPL
- Ruby
- Sidef
- Swift
- Wren
- Wren-math
- XPL0
- Zkl