Created
May 12, 2012 14:54
-
-
Save onlyshk/2666984 to your computer and use it in GitHub Desktop.
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
package proj_euler | |
/* | |
* 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. | |
*/ | |
//906609 | |
object Problem4 { | |
def solve() : Int = { | |
// generate seq from 100 to 999 | |
var r1 = Range(100, 1000).toList | |
// generate seq from 100 to 999 | |
var r2 = Range(100, 1000).toList | |
// combine all elements (Int, Int) | |
val r3 = r1.map(elem1 => r2.map(elem2 => (elem1, elem2 ))).flatten | |
// filter palindrome | |
val r4 = r3.filter(elem => | |
is_palin(elem._1 * elem._2) == true) | |
// find max palindrome | |
val max = r4.map(elem => elem._1 * elem._2).max | |
return max | |
} | |
/* | |
* check is 'n' palindrome or not | |
*/ | |
def is_palin(n : Int) : Boolean ={ | |
var p = n.toString.reverse | |
if (n.toString == p) | |
return true | |
else | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment