[KnowIt - 2020] Day Two - Add Lua solution

This commit is contained in:
Alex Thomassen 2020-12-02 19:42:49 +01:00
parent 91cef0b791
commit 9a6ef8225b
Signed by: Alex
GPG Key ID: 10BD786B5F6FF5DE

View File

@ -0,0 +1,45 @@
local min = 0
local max = 5433000
local total = 0
local function isPrime(num)
if num <= 2 then
return false
end
if num % 2 == 0 then
return false
end
for i = 2, (num - 1) do
if num % i == 0 then
return false
end
end
return true
end
-- Finn nærmeste primtall som er lavere enn `max`
local function findPrime(max)
for count = max, 3, -1 do
if isPrime(count) then
return count
end
end
end
local gifts = min
while gifts < max do
local hasSeven = tostring(gifts):match('7')
if hasSeven ~= nil then
gifts = gifts + findPrime(gifts)
else
total = total + 1
end
gifts = gifts + 1
end
print(total)