Created
January 22, 2013 12:34
-
-
Save zerosum/4594315 to your computer and use it in GitHub Desktop.
Project Euler: Problem 14
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 | |
| object Euler0014 { | |
| def main(args: Array[String]) { | |
| println( | |
| (1 until 1000000).map(x => solveCollatz(x, x, 0)).maxBy(_._2)._1 | |
| ) | |
| } | |
| def solveCollatz(i: Long, tmp: Long, counter: Long): (Long, Long) = { | |
| if (tmp == 1) { | |
| (i, counter) | |
| } else if (tmp%2 == 0) { | |
| solveCollatz(i, tmp/2, counter+1) | |
| } else { | |
| solveCollatz(i, tmp*3+1 ,counter+1) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment