Sequence: smallest number greater than previous term with exactly n divisors: Difference between revisions
Content added Content deleted
imported>Chinhouse No edit summary |
(Added various BASIC dialects (BASIC256, Gambas, PureBasic, QBasic, Run BASIC, XBasic and Yabasic)) |
||
Line 292: | Line 292: | ||
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}}== |