Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Test fib

# Hello World Program in Ruby
# fib(5) = 0,1,1,2,3,5
fib(9) = 0,1,1,2,3,5,8
*/


def fib(num) 
    acc=[]
    (1..num).each do |i|
      acc << 0 if i==1 
      acc << 1 if i==2 
      acc << acc[i-3] + acc[i-2] if i > 2
    end
    acc
end
puts fib(5).join(', ')
puts fib(9).join(', ')

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.