From: "Kroeger, Simon (ext)" Date: 2006-07-14T18:13:59+09:00 Subject: Re: Recursion and Ruby > From: Glenn Cadman > Sent: Friday, July 14, 2006 10:50 AM > > > BTW How would I then access $fib to puts an output of the > sequence eg. 0 > > 1 1 2 3 5 8 13 etc. > > The best I could do was something like: > > printf("Enter a number: ") > i = gets > puts "The #{i.to_i} Fibonacci number is #{fib(i.to_i)} " > 0.upto(i.to_i){|k| printf(" #{$fib[k]}") } maybe ---------------------------------------------- fib = Hash.new{|h,k| h[k] = h[k-2] + h[k-1]} fib[0], fib[1] = 0, 1 print "Enter a number: " i = gets.to_i puts "The #{i} Fibonacci number is #{fib[i]}" puts (0..i).map{|k| fib[k]}.join(' ') ---------------------------------------------- cheers Simon