Created
February 19, 2013 11:32
-
-
Save zerosum/4985049 to your computer and use it in GitHub Desktop.
Project Euler: Problem 92
This file contains hidden or 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
| package euler.zerosum | |
| import euler.zerosum.Number._ | |
| object Euler0092 { | |
| def main(args: Array[String]) { | |
| val n = from(0).take(1000) | |
| println( | |
| n | |
| .par | |
| .map(x => { | |
| from(x*10000 + 1).take(10000).par.filterNot(terminateDigitChain(_) == 1).length | |
| }) | |
| .sum) | |
| } | |
| def terminateDigitChain(s: Int): Int = { | |
| val d = sumOfSquareDigit(s) | |
| if (d == 1 || d == 85 || d == 58) d else terminateDigitChain(d) | |
| } | |
| def sumOfSquareDigit(n: Int): Int = { | |
| println(n) | |
| n.toString.toList.map(_.toInt - 48).fold(0)((sum, x) => sum + x * x) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment