fibonacci series in Ruby


The Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The next number is found by adding up the two numbers before it.
  • The 2 is found by adding the two numbers before it (1+1)
  • Similarly, the 3 is found by adding the two numbers before it (1+2),
  • And the 5 is (2+3),
  • and so on!

Fibonacci series in Ruby

class Fibonacci
  def series
    puts "Enter the Fibonacci value"
    n=gets.to_i
    f1=0
    f2=1
    f3=0
        while f3<n do 
          f3=f1+f2
          puts f3
          f1=f2
          f2=f3  
       end
    end
end
obj1=Fibonacci.new
obj1.series

Comments

Post a Comment

Popular posts from this blog

how to customize dashboard in active admin gem in rails 4

Write a program to Sum of Digits in JQuery?