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

m
imported>Chinhouse
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by 2 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 2,302 ⟶ 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,431 ⟶ 2,627:
=={{header|Wren}}==
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var limit = 22
9,482

edits