Skip to content

Instantly share code, notes, and snippets.

@danvonk
Created October 13, 2014 20:02
Show Gist options
  • Save danvonk/c3634f557707eff09ed2 to your computer and use it in GitHub Desktop.
Save danvonk/c3634f557707eff09ed2 to your computer and use it in GitHub Desktop.
euler14
#!/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