[OT] fastest fibbonacci
Era Scarecrow via Digitalmars-d
digitalmars-d at puremagic.com
Mon Oct 24 12:03:51 PDT 2016
On Monday, 24 October 2016 at 08:54:38 UTC, Andrea Fontana wrote:
> You can simply write it as:
> round(phi^n/sqrt(5));
>
> Check my example above :)
Ran your example and it's perfect for 32bit code. But 64bit, not
so much. It's only good through 71 iterations (longs) then it
starts having errors. Also for some odd reason the input is one
off, so i had to add a -1 to the input for it to align. This
makes it accurate to 41/64 bit results.
for(int i = 1; i < 100; ++i) {
auto cf = computeFib(i);
auto cfm = computeFibMagic(i-1); //with magic numbers exampled
writeln(i, ": ", cf, "\t", cfm, "\t", cf == cfm);
}
64: 10610209857723 10610209857723 true
65: 17167680177565 17167680177565 true
66: 27777890035288 27777890035288 true
67: 44945570212853 44945570212853 true
68: 72723460248141 72723460248141 true
69: 117669030460994 117669030460994 true
70: 190392490709135 190392490709135 true
71: 308061521170129 308061521170129 true
72: 498454011879264 498454011879265 false
73: 806515533049393 806515533049395 false
74: 1304969544928657 1304969544928660 false
75: 2111485077978050 2111485077978055 false
76: 3416454622906707 3416454622906715 false
77: 5527939700884757 5527939700884771 false
78: 8944394323791464 8944394323791487 false
79: 14472334024676221 14472334024676258 false
80: 23416728348467685 23416728348467746 false
More information about the Digitalmars-d
mailing list