repl.it
@anonymous/

BlondWaryShareware

Lua

No description

fork
loading
Files
  • main.lua
main.lua
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
s = io.read()
function ispal(a)
  for i=1,math.floor(a.len(a)/2)+1 do
    if (a.sub(a,i,i) ~= a.sub(a,a.len(a)-i+1,a.len(a)-i+1)) then
      return false
    end
  end
  return true
end

function getpals(s)
  for i=s,1,-1 do -- Decrement and check if palindrome
    if (ispal(tostring(i))) then
      t=i
      for j=s-i,1,-1 do -- If it is, subtract from the original number and decrement the difference
        if (ispal(tostring(j))) then
          u=j
          for k=t-j,0,-1 do -- Again, only from the second number
            if (ispal(tostring(k)) and ((t + u + k) == tonumber(s))) then
              print(s.format("%.0f",t))
              print(s.format("%.0f",u))
              print(s.format("%.0f",k))
              return
            end
          end
        end
      end      
    end
  end
end

if (ispal(s)) then print(s) else getpals(s) end
?