Ruby/problem014.rb
# NOTE: Once the chain starts the terms are allowed to go above one million.
Once the chain starts the terms are allowed to go above one million.
Once the chain starts the terms are allowed to go above one million .
def chain(number, collatz_hash)
  return collatz_hash[number] if collatz_hash[number]
  if number.even?
    collatz_hash[number] = 1 + chain(number/2, collatz_hash)
  elsif number.odd?
    collatz_hash[number] = 1 + chain((3*number + 1), collatz_hash)
  end
end
def chain ( number , collatz_hash ) return collatz_hash [ number ] if collatz_hash [ number ] if number . even? collatz_hash [ number ] = 1 + chain ( number / 2 , collatz_hash ) elsif number . odd? collatz_hash [ number ] = 1 + chain ( ( 3 * number + 1 ) , collatz_hash ) end end
NOTE: Once the chain starts the terms are allowed to go above one million.
NOTE : Once the chain starts the terms are allowed to go above one million .
number
<not_specific>
collatz_hash
<not_specific>