Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. # File: fibonacci-3.rb
  2. #
  3. require 'benchmark'
  4.  
  5. def fibonacci(n)
  6. return 1 if (1..2).include?(n)
  7. fibonacci(n - 1) + fibonacci(n - 2)
  8. end
  9.  
  10. print 'Give the fibonacci position you want to calculate fibonacci for (between 1 and 40): '
  11. n = gets.to_i
  12.  
  13. if n < 1 || n > 40
  14. puts "Please, give an integer between 1 and 40."
  15. exit(1)
  16. end
  17.  
  18. fib = nil
  19. Benchmark.bm do |x|
  20. x.report { fib = fibonacci(n) }
  21. end
  22. puts "The fibonacci(#{n}) is: #{fib}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement