brewersfan1976

fibonacci_number.rb

Sep 4th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.46 KB | None | 0 0
  1. def fibonacciNumber(n)
  2.     if n == 1 then
  3.        return 1
  4.     end
  5.        
  6.     if n == 2 then
  7.        return 1
  8.     end
  9.        
  10.     x = 2
  11.     value_before = 1
  12.     second_value_before = 0
  13.     output_value = 0
  14.        
  15.     while (x <= n)
  16.           output_value = value_before + second_value_before
  17.           second_value_before = value_before
  18.           value_before = output_value
  19.           x = x + 1
  20.     end
  21.        
  22.     return output_value
  23. end
Advertisement
Add Comment
Please, Sign In to add comment