Created
October 13, 2014 20:02
-
-
Save danvonk/c3634f557707eff09ed2 to your computer and use it in GitHub Desktop.
euler14
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
#!/usr/bin/env ruby | |
require 'benchmark' | |
def collatzSequence(x) # e.g. 916 | |
n = x | |
chain = 0 | |
loop do | |
if n % 2 == 0 | |
n = n/2 | |
chain = chain + 1 | |
else | |
n = 3 * n + 1 | |
chain = chain + 1 | |
end | |
break if n == 1 | |
end | |
return chain + 1 | |
end | |
def doTest() | |
#start from 1m and work backwards? | |
count = 1_000_000 | |
record = 0 #the biggest chain so far... | |
loop do | |
c = collatzSequence(count) | |
if c > record | |
c = record | |
end | |
count = count - 1 | |
end | |
end | |
puts Benchmark.measure { doTest() } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment