Skip to content

Instantly share code, notes, and snippets.

@motokiee
Created September 24, 2015 11:01
Show Gist options
  • Save motokiee/a8d8865b7d60ef096dd6 to your computer and use it in GitHub Desktop.
Save motokiee/a8d8865b7d60ef096dd6 to your computer and use it in GitHub Desktop.
func chain(num:Int) -> [Int] {
guard num > 0 else {
fatalError("can not calculate with 0")
}
switch num {
case 1:
return [1]
case let num where num % 2 == 0:
return [num] + chain(num/2)
default:
return [num] + chain(num*3+1)
}
}
(1...100).map { (num:Int) -> [Int] in
return chain(num)
}.filter { (nums:[Int]) -> Bool in
nums.count > 15
}.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment