Skip to content

Instantly share code, notes, and snippets.

@paralax
Created May 18, 2015 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paralax/8eee743d33c21fd05f09 to your computer and use it in GitHub Desktop.
Save paralax/8eee743d33c21fd05f09 to your computer and use it in GitHub Desktop.
sad cycle detector
def sadCycle(n:Int, b:Int): List[Int] = {
def transform(n:Int, b:Int): Int =
n.toString.toCharArray.map(x => (x.toInt-48, b)).map(x => scala.math.pow(x._1, x._2)).sum.toInt
def loop(n:Int, b:Int, acc:List[Int]): List[Int] = {
(acc.tail contains acc.head) match {
case true => acc
case false => loop(transform(n, b), b, transform(n, b)::acc)
}
}
loop(n, b, List(0))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment