RosettaCodeData/Task/Isqrt-integer-square-root-of-X/FreeBASIC/isqrt-integer-square-root-of-x.basic
2023-07-01 13:44:08 -04:00

40 lines
813 B
Text

function isqrt( byval x as ulongint ) as ulongint
dim as ulongint q = 1, r
dim as longint t
while q <= x
q = q shl 2
wend
while q > 1
q = q shr 2
t = x - r - q
r = r shr 1
if t >= 0 then
x = t
r += q
end if
wend
return r
end function
function commatize( byval N as string ) as string
dim as string bloat = ""
dim as uinteger c = 0
while N<>""
bloat = right(N,1) + bloat
N = left(N, len(N)-1)
c += 1
if c mod 3 = 0 and N<>"" then bloat = "," + bloat
wend
return bloat
end function
for i as ulongint = 0 to 65
print isqrt(i);" ";
next i
print
dim as string ns
for i as uinteger = 1 to 22 step 2
ns = str(isqrt(7^i))
print i, commatize(ns)
next i