a guest Sep 14th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- # find the nth value in the fibonacci squence
- require "matrix"
- # analyze processing time
- require 'Benchmark'
- time = Benchmark.realtime do
- # get the user input for n, the nth value to find
- n = gets.to_i
- # Donald Knuth's Q matrix
- def fib(n)
- # find the nth power of the Q matrix, return row 0, column 1
- (Matrix[[1,1], [1,0]] ** n)[0,1]
- puts fib(n).to_s
- puts time.to_s + " elapsed time"
RAW Paste Data