RSS

================================================================================

Before Today

================================================================================

If I was crying
In the van, with my friend
It was for freedom
From myself and from the land

================================================================================

09/23/2010 00:45:16

Scala | collatz2

def collatz(n: Int, list: List[Int] = Nil): List[Int] = n match {
  case 1               => (n :: list).reverse
  case n if n % 2 == 0 => collatz(n / 2, n :: list)
  case _               => collatz(3 * n + 1, n :: list)
}

println(collatz(69)) // => List(69, 208, 104, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1)

--------------------------------------------------------------------------------

 

================================================================================

Designed: Robert Boylan
Powered: Tumblr