Skip to content

Instantly share code, notes, and snippets.

@rafaeldff
Created August 22, 2010 17:21
Show Gist options
  • Save rafaeldff/544019 to your computer and use it in GitHub Desktop.
Save rafaeldff/544019 to your computer and use it in GitHub Desktop.
def happy_?(i:Int) = {
def sumOfSquares(n:Int) = n.toString.map(_ - '0').map(d=>d*d).sum
val numbers = Stream.iterate(i)(sumOfSquares)
def contents:Stream[Set[Int]] = Set[Int]() #:: (numbers zip contents).map{case (n, s) => s + n}
val stopNumber = (numbers zip contents) find {case (n,s) => n == 1 || s.contains(n)}
stopNumber match {case Some((1,_)) => "happy"; case _ => "sad"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment