#!/usr/bin/env ruby # Copyright 2005 Daniel Cer (daniel.cer@cs.colorado.edu) # # This work is licensed under the Creative Commons Attribution-NonCommercial- # ShareAlike License. To view a copy of this license, visit # http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to # Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, # 94105, USA. def fibonacci(n) return -1 if n <= 0; fib = prev_fib = 1; n=n-2; n.times { next_prev_fib = fib; fib = fib+prev_fib; prev_fib = next_prev_fib } fib end if (ARGV.length != 1) $stderr.puts "Usage:\n\t#{$0} (n-th fibonacci # to calculate)" exit -1 end n = ARGV[0].to_i puts "fibonacci(#{n}): #{fibonacci(n)}"