Jump to content

Magic constant: Difference between revisions

Magic constant in various BASIC dialents
(Magic constant en Python)
(Magic constant in various BASIC dialents)
Line 196:
10^13: 27,145
</pre>
 
==={{header|BASIC256}}===
<lang BASIC256>function a(n)
n = n + 2
return n*(n^2 + 1)/2
end function
 
function inv_a(x)
k = 0
while k*(k^2+1)/2+2 < x
k += 1
end while
return k
end function
 
print "The first 20 magic constants are:"
for n = 1 to 20
print int(a(n));" ";
next n
print : print
print "The 1,000th magic constant is "; int(a(1000)); chr(10)
 
for e = 1 to 20
print "10^"; e; ": "; chr(9); inv_a(10^e)
next e
end</lang>
 
==={{header|PureBasic}}===
<lang PureBasic>Procedure.i a(n.i)
n + 2
ProcedureReturn n*(Pow(n,2) + 1)/2
EndProcedure
 
Procedure.i inv_a(x.i)
k.i = 0
While k*(Pow(k,2)+1)/2+2 < x
k + 1
Wend
ProcedureReturn k
EndProcedure
 
OpenConsole()
PrintN("The first 20 magic constants are:")
For n.i = 1 To 20
Print(Str(a(n)) + " ")
Next n
PrintN("") : PrintN("")
PrintN("The 1,000th magic constant is " + Str(a(1000)))
 
For e.i = 1 To 20
PrintN("10^" + Str(e) + ": " + #TAB$ + Str(inv_a(Pow(10,e))))
Next e
CloseConsole()</lang>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<lang QBasic>FUNCTION a (n)
n = n + 2
a = n * (n ^ 2 + 1) / 2
END FUNCTION
 
FUNCTION inva (x)
k = 0
WHILE k * (k ^ 2 + 1) / 2 + 2 < x
k = k + 1
WEND
inva = k
END FUNCTION
 
PRINT "The first 20 magic constants are: ";
FOR n = 1 TO 20
PRINT a(n); " ";
NEXT n
PRINT
PRINT "The 1,000th magic constant is "; a(1000)
PRINT
FOR e = 1 TO 20
PRINT USING "10^##: #########"; e; inva(10 ^ e)
NEXT e
END</lang>
 
==={{header|True BASIC}}===
<lang qbasic>FUNCTION a(n)
LET n = n + 2
LET a = n*(n^2 + 1)/2
END FUNCTION
 
FUNCTION inv_a(x)
LET k = 0
DO WHILE k*(k^2+1)/2+2 < x
LET k = k + 1
LOOP
LET inv_a = k
END FUNCTION
 
PRINT "The first 20 magic constants are: ";
FOR n = 1 TO 20
PRINT a(n);" ";
NEXT n
PRINT
PRINT "The 1,000th magic constant is "; a(1000)
 
FOR e = 1 TO 20
PRINT USING "10^##": e;
PRINT USING": #########": inv_a(10^e)
NEXT e
END</lang>
 
==={{header|Yabasic}}===
<lang yabasic>sub a(n)
n = n + 2
return n*(n^2 + 1)/2
end sub
sub inv_a(x)
k = 0
while k*(k^2+1)/2+2 < x
k = k + 1
wend
return k
end sub
print "The first 20 magic constants are: "
for n = 1 to 20
print a(n), " ";
next n
print "\nThe 1,000th magic constant is ", a(1000), "\n"
for e = 1 to 20
print "10^", e using"##", ": ", inv_a(10^e) using "#########"
next e
end</lang>
 
 
=={{header|Factor}}==
2,133

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.