Skip to content

Instantly share code, notes, and snippets.

@johnpaulmanoza
Last active August 29, 2015 14:27
Show Gist options
  • Save johnpaulmanoza/2d60147018d9d7e37618 to your computer and use it in GitHub Desktop.
Save johnpaulmanoza/2d60147018d9d7e37618 to your computer and use it in GitHub Desktop.
Collatz Sequence Longest Chain Number
// 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