25 lines
1.5 KiB
Text
25 lines
1.5 KiB
Text
--[[
|
|
The answer must be an even number and it can't be less than the square root of 269,696.
|
|
So, if we start from that, keep on adding 2 and squaring it we'll eventually find the answer.
|
|
However, we can skip numbers which don't end in 4 or 6 as their squares can't end in 6.
|
|
]]
|
|
|
|
local fmt = require "fmt" -- this enables us to format numbers with thousand separators
|
|
local start = math.sqrt(269696) -- get the square root of the starting value
|
|
start = math.ceil(start) -- get the next integer higher than (or equal to) the square root
|
|
start = math.ceil(start / 2) * 2 -- if it's odd, use the next even integer
|
|
local i = start -- assign it to a variable 'i' for use in the following loop
|
|
while true do -- loop indefinitely till we find the answer
|
|
local sq = i * i -- get the square of 'i'
|
|
local last6 = sq % 1000000 -- get its last 6 digits by taking the remainder after division by a million
|
|
if last6 == 269696 then -- if those digits are 269696, we're done and can print the result
|
|
fmt.print($"The lowest number whose square ends in 269,696 is %,s.", i)
|
|
fmt.print($"Its square is %,s.", sq)
|
|
break -- break from the loop and end the program
|
|
end
|
|
if i % 10 == 6 then -- get the last digit by taking the remainder after division by 10
|
|
i += 8 -- if the last digit is 6 add 8 (to end in 4)
|
|
else
|
|
i += 2 -- otherwise add 2
|
|
end
|
|
end
|