Thursday, March 12, 2015

Problem 4 in Scala

/**
  * Largest palindrome product: https://projecteuler.net/problem=4
  *
  * TA 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.
  */

val allnum = 100 to 999
val allprod = for (x <- allnum; y <- allnum) yield {(x, y, x * y)}
val answer = allprod.
  filter(x => (x._3 == x._3.toString.reverse.toInt)).
  sortWith(_._3 > _._3).head

No comments:

Post a Comment