Project

General

Profile

Feature #1218 » fibonacci-recursive.rb

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

 
#!/usr/bin/env ruby -w

# compute nth fibonacci number in ruby using tail-recursion

def fibonacci( n )
if n == 0 || n == 1
n
else
fibonacci( n - 1 ) + fibonacci( n - 2 )
end
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-recusive.rb <integer>"
end

end
(2-2/3)