-
-
Save Azolo/c673b675aeea5119ef9bf1fe6fa5aa4d to your computer and use it in GitHub Desktop.
Euler Plaindrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PalindromeNum do | |
def largest_palindrome(upper_bound, lower_bound) do | |
find_largest({upper_bound, upper_bound}, 0, lower_bound) | |
end | |
defp find_largest({x, _}, largest_num, lower_bound) when x <= lower_bound, do: largest_num | |
defp find_largest({x, y}, largest_num, lower_bound) when y <= lower_bound do | |
find_largest({x-1, x-1}, largest_num, lower_bound) | |
end | |
defp find_largest({x, y}, largest_num, _) when x == y and x * y < largest_num, do: largest_num | |
defp find_largest({x, y}, largest_num, lower_bound) when x * y < largest_num do | |
find_largest({x-1, x-1}, largest_num, lower_bound) | |
end | |
defp find_largest({x, y}, largest_num, lower_bound) do | |
test_num = x*y | |
if palindrome?(test_num) do | |
find_largest({x-1,x-1}, test_num, lower_bound) | |
else | |
find_largest({x, y-1}, largest_num, lower_bound) | |
end | |
end | |
def palindrome?(num) do | |
list = Integer.digits(num) | |
list == Enum.reverse(list) | |
end | |
end | |
IO.inspect PalindromeNum.largest_palindrome(999, 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment