-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp4.lua
More file actions
38 lines (33 loc) · 962 Bytes
/
p4.lua
File metadata and controls
38 lines (33 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env lua
--[[
A palindromic number reads the same both ways. The largest palindrome made
from the product of two 2-digit numbers is 9009 = 91 * 99.
Find the largest palindrome made from the product of two 3-digit numbers.
]]
--- Check if a number is a palindrome.
---@param m number Check if this number is a palindrome or not.
local function is_palindrome(m)
local s = tostring(m)
if s == string.reverse(s) then
return true
else
return false
end
end
--- Find the max number that is a palindrome and the product of two
--- 3-digit numbers.
local function max_palindrome()
local current_max = 0
for i = 999, 100, -1 do
for j = i, 999 do
if i * j > current_max then
if is_palindrome(i * j) then
current_max = i * j
end
end
end
end
return current_max
end
-- Compute solution
print(max_palindrome()) -- 906,609