Created
December 12, 2011 01:09
-
-
Save abdollar/1463968 to your computer and use it in GitHub Desktop.
Find the largest palindrome made from the product of two 3-digit numbers
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
def palindrome?(str); str.reverse == str; end | |
max = 0 | |
900.upto(999) { |a| | |
a.upto(999) { |b| | |
max = [max, a * b].max if palindrome?((a * b).to_s) | |
} | |
} | |
max | |
array = (900..999).to_a | |
array.inject([0]) { |i,sum| i += array.map { |n| sum*n } }.select{|v| palindrome?(v.to_s)}.uniq.sort.last |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment