daily pastebin goal
56%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top