Fibonacci matrix-exponentiation: Difference between revisions
Content deleted Content added
Line 1,706: | Line 1,706: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
===Matrix exponentiation by Ruby's fast exponentiation operator duck-typing applied to Ruby's built-in Integer Class=== |
===Matrix exponentiation by Ruby's fast exponentiation operator duck-typing applied to Ruby's built-in Integer Class=== |
||
<pre> |
|||
require 'matrix' |
require 'matrix' |
||
start_time = Time.now |
start_time = Time.now |
||
[0,1,2,3,4,10,100,256, 1_000, 1024, 10_000, 2 ** 16, 100_000, 1_000_000,10_000_000 ].each {|n| |
[0,1,2,3,4,10,100,256, 1_000, 1024, 10_000, 2 ** 16, 100_000, 1_000_000,10_000_000 ].each {|n| |
||
## |
|||
fib_Num=(Matrix[[0,1],[1,1]] ** (n))[0,1] |
fib_Num=(Matrix[[0,1],[1,1]] ** (n))[0,1] ## Matrix exponentiation |
||
## |
|||
fib_Str= fib_Num.to_s() |
fib_Str= fib_Num.to_s() |
||
if fib_Str.length <= 21 |
if fib_Str.length <= 21 |
||
Line 1,719: | Line 1,721: | ||
} |
} |
||
puts "Took #{Time.now - start_time}s" |
puts "Took #{Time.now - start_time}s" |
||
</pre> |
|||
{{out}} |
{{out}} |