Last active
February 25, 2017 13:24
-
-
Save stulevine/ed9c1096f7038935ab107f22a0babee6 to your computer and use it in GitHub Desktop.
Collatz Conjecture - https://en.wikipedia.org/wiki/Collatz_conjecture
This file contains 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
//: Playground - noun: a place where people can play | |
import UIKit | |
var hits = [Int: Bool]() | |
func collatz(_ n: Int) { | |
if n == 1 { return } | |
var new: Int! | |
if (n % 2) == 0 { | |
new = n / 2 | |
} | |
else { | |
new = 3*n + 1 | |
} | |
if hits[n] == nil { | |
hits[n] = true | |
print("\(n)") | |
} | |
collatz(new) | |
} | |
collatz(2357693) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment