Created
April 25, 2012 19:59
-
-
Save ryanoneill/2492903 to your computer and use it in GitHub Desktop.
Project Euler - Problem 04 - 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
// http://projecteuler.net/problem=4 | |
// A palindromic number reads the same both ways. The largest palindrome made | |
// from the product of two 2-digit numbers is 9009 = 91 99. | |
// Find the largest palindrome made from the product of two 3-digit numbers. | |
object Problem04 { | |
def main(args: Array[String]) { | |
val twoDigitNumbers = (10 to 99).toList | |
println("Largest Palindrome Product of 2-Digit Numbers: " + maxPalindromeProduct(twoDigitNumbers)) | |
val threeDigitNumbers = (100 to 999).toList | |
println("Largest Palindrome Product of 3-Digit Numbers: " + maxPalindromeProduct(threeDigitNumbers)) | |
} | |
def maxPalindromeProduct(numbers: List[Int]) = { | |
val palindromes = | |
for { | |
a <- numbers | |
b <- numbers | |
value = a * b | |
if (value.toString == value.toString.reverse) | |
} yield value | |
palindromes.max | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment