Skip to content

Instantly share code, notes, and snippets.

@radar
Forked from chendo/gist:91607
Created April 8, 2009 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radar/91642 to your computer and use it in GitHub Desktop.
Save radar/91642 to your computer and use it in GitHub Desktop.
# http://mozy.com/contest
# Question 4
# chendo's solution
require 'benchmark'
year = 0
fibs = [2, 0, 0, 0, 0] # age 0, 1, 2, 3, 4
adults = 0 # age > 5
Benchmark.bm do |bm|
bm.report do
while year < 75000
year += 1
fibs.unshift(adults += fibs.pop)
end
puts "At #{year} years, there would be #{adults + fibs.inject(0) { |mem, i| mem += i }}"
end
end
# user system total real
# At 75 years, there would be 1457183502
# 0.000000 0.000000 0.000000 ( 0.000675)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment