0 Replies - 1217 Views - Last Post: 26 November 2009 - 10:49 AM

#1 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Recursive Fibonacci

Posted 26 November 2009 - 10:49 AM

Description: Provides 2 simple functions: one which finds the nth element of the Fibonacci series (fib), and the other, which prints out the entire Fibonacci series up to that number (printFib)
def fib(num)
  if num <= 1
    return 1
  end
  return (fib(num-1)+fib(num-2))
end

def printFib(num)
  for x in 0..num
    print "#{fib(x)} "
  end 
  puts  
end

#example
printFib(10) #=> 1 1 2 3 5 8 13 21 34 55 89 

Is This A Good Question/Topic? 0
  • +

Page 1 of 1