Sequence: smallest number with exactly n divisors: Difference between revisions

m
(Added PROMAL)
m (→‎{{header|Wren}}: Minor tidy)
 
(8 intermediate revisions by 6 users not shown)
Line 242:
first 15 terms: 1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|Ring}}
<syntaxhighlight lang="vbnet">print "the first 15 terms of the sequence are:"
for n = 1 to 15
for m = 1 to 4100
pnum = 0
for p = 1 to 4100
if m % p = 0 then pnum += 1
next p
if pnum = n then
print m; " ";
exit for
end if
next m
next n</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{trans|Ring}}
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="vbnet">100 print "the first 15 terms of the sequence are:"
110 for n = 1 to 15
120 for m = 1 to 4100
130 pnum = 0
140 for p = 1 to 4100
150 if m mod p = 0 then pnum = pnum+1
160 next p
170 if pnum = n then print m;" "; : goto 190
180 next m
190 next n
200 print "done..."
210 end</syntaxhighlight>
 
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|QBasic}}===
{{trans|Ring}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">PRINT "the first 15 terms of the sequence are:"
FOR n = 1 to 15
FOR m = 1 to 4100
pnum = 0
FOR p = 1 to 4100
IF m MOD p = 0 then pnum = pnum + 1
NEXT p
IF pnum = n then
PRINT m;
EXIT FOR
END IF
NEXT m
NEXT n</syntaxhighlight>
 
==={{header|Run BASIC}}===
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|True BASIC}}===
{{trans|Ring}}
<syntaxhighlight lang="qbasic">PRINT "the first 15 terms of the sequence are:"
FOR n = 1 to 15
FOR m = 1 to 4100
LET pnum = 0
FOR p = 1 to 4100
IF remainder(m, p) = 0 then LET pnum = pnum+1
NEXT p
IF pnum = n then
PRINT m;
EXIT FOR
END IF
NEXT m
NEXT n
PRINT "done..."
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{trans|Ring}}
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "program name"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
PRINT "the first 15 terms of the sequence are:"
FOR n = 1 TO 15
FOR m = 1 TO 4100
pnum = 0
FOR p = 1 TO 4100
IF m MOD p = 0 THEN INC pnum
NEXT p
IF pnum = n THEN
PRINT m;
EXIT FOR
END IF
NEXT m
NEXT n
END FUNCTION
END PROGRAM</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, nfound
 
UPTO = 15
i = 2
nfound = 1
Dim sfound.i(UPTO)
sfound(1) = 1
 
While nfound < UPTO
n = divisors(i)
If n <= UPTO And sfound(n) = 0:
nfound + 1
sfound(n) = i
EndIf
i + 1
Wend
 
Print("1 ") ;special case
For i = 2 To UPTO
Print(Str(sfound(i)) + " ")
Next i
 
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">UPTO = 15
i = 2
nfound = 1
dim sfound(UPTO)
sfound(1) = 1
 
while nfound < UPTO
n = divisors(i)
if n <= UPTO and sfound(n) = 0 then
nfound = nfound + 1
sfound(n) = i
fi
i = i + 1
end while
 
print 1, " "; //special case
for i = 2 to UPTO
print sfound(i), " ";
next i
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|BCPL}}==
Line 411 ⟶ 596:
{{out}}
<pre>1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
{This code would normally be in a separate library. It is provided 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 SequenceWithNdivisors(Memo: TMemo);
var N,N2: integer;
var IA: TIntegerDynArray;
begin
for N:=1 to 15 do
begin
N2:=0;
repeat Inc(N2) until N=GetAllDivisors(N2,IA);
Memo.Lines.Add(IntToStr(N)+' - '+IntToStr(N2));
end;
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
1 - 1
2 - 2
3 - 4
4 - 6
5 - 16
6 - 12
7 - 64
8 - 24
9 - 36
10 - 48
11 - 1024
12 - 60
13 - 4096
14 - 192
15 - 144
 
Elapsed Time: 54.950 ms.
 
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func cntdiv n .
i = 1
while i <= sqrt n
if n mod i = 0
cnt += 1
if i <> sqrt n
cnt += 1
.
.
i += 1
.
return cnt
.
len seq[] 15
i = 1
while n < 15
k = cntdiv i
if k <= 15 and seq[k] = 0
seq[k] = i
n += 1
.
i += 1
.
for v in seq[]
print v
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 1,022 ⟶ 1,311:
14 192
15 144</pre>
 
=={{header|MiniScript}}==
<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 = []
for i in range(1, 15)
j = 1
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, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (show (take 15 a005179) ++ "\n")]
 
a005179 :: [num]
a005179 = map smallest_n_divisors [1..]
 
smallest_n_divisors :: num->num
smallest_n_divisors n = hd [i | i<-[1..]; n = #divisors i]
 
divisors :: num->[num]
divisors n = [d | d<-[1..n]; n mod d=0]</syntaxhighlight>
{{out}}
<pre>[1,2,4,6,16,12,64,24,36,48,1024,60,4096,192,144]</pre>
 
=={{header|Modula-2}}==
Line 1,861 ⟶ 2,198:
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49g}}
≪ {1}
2 ROT '''FOR''' j
1
'''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 12 64 24 36 48 1024 60 4096 192 144}
</pre>
Runs in 13 minutes on an HP-50g.
====Faster implementation====
With the hint given in the [https://oeis.org/A005179 OEIS page] that a(p)=2^(p-1) when p is prime:
≪ {1}
2 ROT '''FOR''' j
'''IF''' j ISPRIME? '''THEN''' 2 j 1 - ^
'''ELSE'''
1 '''DO''' 1 + '''UNTIL''' DUP DIVIS SIZE j == '''END'''
'''END'''
+
'''NEXT '''
≫ '<span style="color:blue">TASK</span>' STO
the same result is obtained in less than a minute.
 
=={{header|Ruby}}==
Line 2,121 ⟶ 2,487:
64 7560
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program smallest_number_with_exactly_n_divisors;
print([a(n) : n in [1..15]]);
 
proc a(n);
return [i +:= 1 : until n = #[d : d in [1..i] | i mod d=0]](i);
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>[1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144]</pre>
 
=={{header|Sidef}}==
Line 2,250 ⟶ 2,627:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var limit = 22
9,476

edits