samskivert: Euler 049

11 August 2008

Problem 049:

object Euler49 extends EulerApp {
  def sort (n :Int) = n.toString.toList.sort(_<_).mkString.toInt
  def isterm (n :Int, nn :Int) = isprime(nn) && sort(nn) == sort(n)
  def isseq (n :Int) = isterm(n, n+3330) && isterm(n, n+6660)
  def mkseq (n :Int) = List(n, n+3330, n+6660).mkString
  println(mkseq(1488.to(10000-6660).filter(isprime).filter(isseq).first))
}

This one's been somewhat deconstructed. I originally just wrote a for loop, but then I wanted to make it a bit more functional. Then I wanted to make it a bit shorter. In the end it's still fairly readable, in an HP calculator, Reverse Polish sort of way.

©1999–2015 Michael Bayne