Last active
August 29, 2015 14:27
-
-
Save johnpaulmanoza/2d60147018d9d7e37618 to your computer and use it in GitHub Desktop.
Collatz Sequence Longest Chain Number
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 million = 1000000 | |
var bestLength = 0 | |
var bestNumber = 0 | |
var number = 0 | |
for i in 2 ... million { | |
var length = 1 | |
number = i | |
while number != 1 { | |
number = number % 2 == 0 ? number / 2 : number * 3 + 1 | |
length++ | |
} | |
if length > bestLength { | |
bestLength = length | |
bestNumber = i | |
} | |
} | |
// Answer is 837799 | |
println("Best Number is \(bestNumber) with Lenght \(bestLength)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment