Smallest square that begins with n: Difference between revisions

Added Lua
(→‎{{header|Free Pascal}}: Julia version is neglectable slower, but uses a lot of memory and only useful for continious regions of numbers to test.)
(Added Lua)
Line 1,814:
41209 4225 4356 441 45369 4624 4761 484 49
27.356 ms (2 allocations: 7.63 MiB)
</pre>
 
=={{header|Lua}}==
{{Trans|Julia}}
<syntaxhighlight lang="lua">
do -- find the smallest square that begins with n for n in 1..49
local function smsq( n )
local results, found, square, delta = {}, 0, 1, 3
while found < n do
local k = square
while k > 0 do
if k <= n and results[ k ] == nil then
results[ k ] = square
found = found + 1
end
k = math.floor( k / 10 )
end
square = square + delta
delta = delta + 2
end
return results
end
 
local seq = smsq( 49 )
for i = 1, #seq do
io.write( " ", string.format( "%5d", seq[ i ] ) )
if i % 10 == 0 then io.write( "\n" ) end
end
end
</syntaxhighlight>
{{out}}
<pre>
1 25 36 4 529 64 729 81 9 100
1156 121 1369 144 1521 16 1764 1849 196 2025
2116 225 2304 2401 25 2601 2704 289 2916 3025
3136 324 3364 3481 35344 36 3721 3844 3969 400
41209 4225 4356 441 45369 4624 4761 484 49
</pre>
 
3,048

edits