Project

General

Profile

Feature #1218 » fibonacci-iterative.rb

This file computes the nth Fibonacci number using iteration. - Anonymous, 02/27/2009 12:13 AM

 
#!/usr/bin/env ruby -w

# compute nth fibonacci number in ruby using iteration.

def fibonacci( n )

a,b = 0,1

n.times do

a,b = b,a+b

end
a

end


if __FILE__ == $0
if ( ARGV.length == 1 ) && ( ARGV.first.to_i.is_a? Integer )
puts fibonacci( ARGV.first.to_i )
else
raise ArgumentError, "Usage: fibonacci-iterative.rb <integer>"
end

end
(1-1/3)