The beauty of sequence comprehensions in Scala
While working through some projecteuler.net problems, I’ve found a quite nice solution to a problem using Scala’s sequence comprehensions.
The problem is this one:
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.
Firstly, a palindrome number is one that reads both ways:
Then use a sequence comprehension to find all the palindrome products, filtering by the
Reads nicely, huh?
And then you can get to the answer to this particular problem like this:
See it in action here.
This could be optimized, of course. That’s just the tip of the iceberg.