Fibonacci matrix-exponentiation: Difference between revisions
Content added Content deleted
Line 1,707: | Line 1,707: | ||
===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=== |
||
require 'matrix' |
|||
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| |
|||
fib_Num=(Matrix[[0,1],[1,1]] ** (n))[0,1] |
fib_Num=(Matrix[[0,1],[1,1]] ** (n))[0,1] |
||
fib_Str= fib_Num.to_s() |
fib_Str= fib_Num.to_s() |
||
if fib_Str.length <= 21 |
if fib_Str.length <= 21 |
||
p [n,fib_Str.length.to_s + ' digits' , fib_Str |
p ["Fibonattch(#{n})",fib_Str.length.to_s + ' digits' , fib_Str] |
||
else |
else |
||
p ["Fibonattch(#{n})",fib_Str.length.to_s + ' digits' , fib_Str.slice(0,20) + " ... " + fib_Str.slice(-20,20)] |
|||
end |
end |
||
} |
|||
puts "Took #{Time.now - start_time}s" |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
["Fibonattch(0)", "1 digits", "0"] |
|||
["Fibonattch(1)", "1 digits", "1"] |
|||
["Fibonattch(2)", "1 digits", "1"] |
|||
[256, "54 digits", "14169381771405651323 ... 1965770779495819986", "Took 0.000390585s"] |
|||
["Fibonattch(3)", "1 digits", "2"] |
|||
[1000, "209 digits", "43466557686937456435 ... 7613779516684922887", "Took 0.000576685s"] |
|||
["Fibonattch(4)", "1 digits", "3"] |
|||
[1024, "214 digits", "45066996336778198131 ... 0410363155392540524", "Took 0.001018916s"] |
|||
["Fibonattch(10)", "2 digits", "55"] |
|||
⚫ | |||
["Fibonattch(100)", "21 digits", "354224848179261915075"] |
|||
⚫ | |||
["Fibonattch(256)", "54 digits", "14169381771405651323 ... 19657707794958199867"] |
|||
["Fibonattch(1000)", "209 digits", "43466557686937456435 ... 76137795166849228875"] |
|||
["Fibonattch(1024)", "214 digits", "45066996336778198131 ... 04103631553925405243"] |
|||
⚫ | |||
Took 1.112902545s |
|||
⚫ | |||
["Fibonattch(100000)", "20899 digits", "25974069347221724166 ... 49895374653428746875"] |
|||
["Fibonattch(1000000)", "208988 digits", "19532821287077577316 ... 68996526838242546875"] |
|||
["Fibonattch(10000000)", "2089877 digits", "11298343782253997603 ... 86998673686380546875"] |
|||
Took 1.115647303s |
|||
</pre> |
</pre> |
||