Lua Implementation of Problem 4

View source code here on GitHub!

Solution

solution()
Returns:

The solution to problem 4

Return type:

number

 1-- Project Euler Problem 4
 2--
 3-- Problem:
 4--
 5-- A palindromic number reads the same both ways. The largest palindrome made from
 6-- the product of two 2-digit numbers is 9009 = 91 × 99.
 7--
 8-- Find the largest palindrome made from the product of two 3-digit numbers.
 9
10return {
11    solution = function()
12        local answer = 0
13
14        for v = 101,999 do
15            for u = 100,(v-1) do
16                local p = u * v
17                local ps = tostring(p)
18
19                if ps == string.reverse(ps) and p > answer then
20                    answer = p
21                end
22            end
23        end
24
25        return answer
26    end
27}

Tags: palindrome