Sequence: smallest number greater than previous term with exactly n divisors: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 6 users not shown)
Line 292:
first 15 terms: 1 2 4 6 16 18 64 66 100 112 1024 1035 4096 4288 4624
</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}}==
Line 395 ⟶ 612:
{{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>
 
=={{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}}==
Line 433 ⟶ 724:
<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|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#}}==
Line 447 ⟶ 764:
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>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: io kernel math math.primes.factors prettyprint sequences ;
Line 859 ⟶ 1,177:
{{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}}==
Line 1,416 ⟶ 1,782:
done...
</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}}==
Line 1,505 ⟶ 1,888:
{{trans|Phix}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var limit = 24
9,479

edits