Created
May 18, 2015 13:14
-
-
Save paralax/8eee743d33c21fd05f09 to your computer and use it in GitHub Desktop.
sad cycle detector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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