Guest User

Untitled

a guest
Sep 14th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.39 KB | None | 0 0
  1. # find the nth value in the fibonacci squence
  2.  
  3. require "matrix"
  4.  
  5. # analyze processing time
  6. require 'Benchmark'
  7. time = Benchmark.realtime do
  8.  
  9. # get the user input for n, the nth value to find
  10. n = gets.to_i
  11.  
  12. # Donald Knuth's Q matrix
  13. def fib(n)
  14. # find the nth power of the Q matrix, return row 0, column 1
  15. (Matrix[[1,1], [1,0]] ** n)[0,1]
  16. end
  17.  
  18. puts fib(n).to_s
  19.  
  20. end
  21.  
  22. puts time.to_s + " elapsed time"
Add Comment
Please, Sign In to add comment