Skip to content

Instantly share code, notes, and snippets.

@Azolo

Azolo/palin.exs Secret

Last active July 11, 2017 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Azolo/c673b675aeea5119ef9bf1fe6fa5aa4d to your computer and use it in GitHub Desktop.
Save Azolo/c673b675aeea5119ef9bf1fe6fa5aa4d to your computer and use it in GitHub Desktop.
Euler Plaindrome
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