Skip to content

Instantly share code, notes, and snippets.

@nokok
Created March 26, 2014 10:20
Show Gist options
  • Save nokok/9780317 to your computer and use it in GitHub Desktop.
Save nokok/9780317 to your computer and use it in GitHub Desktop.
object CodeIQ_Collatz {
val d = (n: Int) => n / 2
val m = (n: Int) => n * 3 + 1
def ev(n: Int) =
if (n % 2 == 0)
d(n)
else
m(n)
def e(i: Int): Boolean = {
var r = m(i)
while (r != 1) {
if (r == i)
return true
r = ev(r)
}
false
}
def main(args: Array[String]) = print(Range(2, 10000, 2).count(p => e(p)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment