Fibonacci matrix-exponentiation: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(8 intermediate revisions by one other user not shown)
Line 1,705:
2^128: 15262728879740471565 ... 17229324095882654267</pre>
=={{header|Ruby}}==
===Matrix exponentiation by Ruby's fast exponentiation operator duck-typing applied to Ruby's built-in Integer Class.===
<pre>
 
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] ## Matrix exponentiation
##
fib_Str= fib_Num.to_s()
if fib_Str.length <= 21
p ["Fibonacci(#{n})",fib_Str.length.to_s + ' digits' , fib_Str, "Took #{Time.now - start_time}s"]
else
p ["Fibonacci(#{n})",fib_Str.length.to_s + ' digits' , fib_Str.slice(0,20) + " ... " + fib_Str[.slice(-20 ... -1] , "Took #{Time.now - start_time}s"20)]
end
}
puts "Took #{Time.now - start_time}s"
 
</pre>
 
{{out}}
<pre>
[10"Fibonacci(0)", "21 digits", "55", "Took 6.836e-05s0"]
[100"Fibonacci(1)", "211 digits", "354224848179261915075", "Took 0.000259665s1"]
["Fibonacci(2)", "1 digits", "1"]
[256, "54 digits", "14169381771405651323 ... 1965770779495819986", "Took 0.000390585s"]
["Fibonacci(3)", "1 digits", "2"]
[1000, "209 digits", "43466557686937456435 ... 7613779516684922887", "Took 0.000576685s"]
["Fibonacci(4)", "1 digits", "3"]
[1024, "214 digits", "45066996336778198131 ... 0410363155392540524", "Took 0.001018916s"]
["Fibonacci(10)", "2 digits", "55"]
[10000, "2090 digits", "33644764876431783266 ... 6607331005994736687", "Took 0.001502226s"]
["Fibonacci(100)", "21 digits", "354224848179261915075"]
[65536, "13696 digits", "73199214460290552832 ... 9727019095530746322", "Took 0.003220941s"]
[100000"Fibonacci(256)", "2089954 digits", "2597406934722172416614169381771405651323 ... 4989537465342874687", "Took 0.006312s19657707794958199867"]
[1000000"Fibonacci(1000)", "208988209 digits", "1953282128707757731643466557686937456435 ... 6899652683824254687", "Took 0.076104599s76137795166849228875"]
[10000000"Fibonacci(1024)", "2089877214 digits", "1129834378225399760345066996336778198131 ... 8699867368638054687", "Took 1.112832817s04103631553925405243"]
["Fibonacci(10000)", "2090 digits", "33644764876431783266 ... 6607331005994736687", "Took 0.001502226s66073310059947366875"]
Took 1.112902545s
["Fibonacci(65536)", "13696 digits", "73199214460290552832 ... 9727019095530746322", "Took 0.003220941s97270190955307463227"]
["Fibonacci(100000)", "20899 digits", "25974069347221724166 ... 49895374653428746875"]
["Fibonacci(1000000)", "208988 digits", "19532821287077577316 ... 68996526838242546875"]
["Fibonacci(10000000)", "2089877 digits", "11298343782253997603 ... 86998673686380546875"]
Took 1.027948065s
</pre>
== Ruby's Matlix#exponentiation duck-typeing with Head-tail-BigNumber class ==
 
===Matrix exponentiation by Ruby's Matlix#exponentiation duck-typeing with Head-tail-BigNumber class ===
Here, the HeadTailBignum class holds the digits of the head part in Ruby's bigDecimal class and tail part in the BigInteger class.
and HeadTailBignum defines only the add and multply operators and the coerce method.
Line 1,814 ⟶ 1,824:
n = (2**h)
fib_Num=(Matrix[[ HeadTailBignum.new(0),1],[1,1]] ** (n))[0,1]
puts "Fibonacci(2^#{h.to_s}) = #{fib_Num.to_s} are #{fib_Num.exponent} degitsdigits"
}
puts "Took #{Time.now - start_time}s"
 
{{out}}
<pre>
Fibonacci(2^8) = 14169381771405651323 ... 19657707794958199867 are 54 degits
Fibonacci(2^168) = 7319921446029055283214169381771405651323 ... 9727019095530746322719657707794958199867 are 1369654 degitsdigits
Fibonacci(2^3216) = 6199931968938185981873199214460290552832 ... 3962373553820807634797270190955307463227 are 89759508013696 degitsdigits
Fibonacci(2^6432) = 1117580753692952842461999319689381859818 ... 1752980034808984018739623735538208076347 are 609335284897595080 degitsdigits
Fibonacci(2^64) = 11175807536929528424 ... 17529800348089840187 are 3855141514259838964 digits
Took 0.009357194s
</pre>
 
=={{header|Raku}}==
Line 1,999 ⟶ 2,011:
{{libheader|Wren-fmt}}
A tough task for Wren which takes just under 3 minutes to process even the 1 millionth Fibonacci number. Judging by the times for the compiled, statically typed languages using GMP, the 10 millionth would likely take north of 4 hours so I haven't attempted it.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./fmt" for Fmt
 
var mul = Fn.new { |m1, m2|
Line 2,117 ⟶ 2,129:
 
Apart from the 2^16th number, the extra credit is still out of reach using this approach.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./fmt" for Fmt
 
var lucas = Fn.new { |n|
Line 2,199 ⟶ 2,211:
{{libheader|Wren-gmp}}
Ridiculously fast (under 5ms) thanks to the stuff borrowed from Sidef and Julia combined with the speed of GMP.
<syntaxhighlight lang="ecmascriptwren">import "./gmp" for Mpz, Mpf
import "./fmt" for Fmt
 
9,482

edits