There are many instances of software developmet where we seem to solve problems differently than from a mathematical perspective. For instance, consider the classical example of calculating Fibonacci numbers. Why do we implement Fibonacci numbers naively using its definition instead of using the explicit formula https://brilliant.org/discussions/thread/the-explicit-formula-for-fibonacci-sequence/. Is there a reason why this is a standard? Should I be conforming to the "standards" of doing it this way in practice? In interviews?
Another class of examples would be optimization problems. Why do we use Dijksttra's algorithm to compute the shortest path as opposed to translating the graph into an integer program and solving that instead? Similarly for the Knapsack problem, why is dynamic programming favored over integer programing techniques developed in mathematical optimization?
fib(94)
overflows aUInt64
. The most performant bet is to pre-compute the first 94 elements (don't forget0
!), shove them a 94-element array, and use a single constant-time index into the array to obtain your results. Only needs 6,016 bytes, which is probably less space than the machine code for implementing Binet's formula, which would also be slower (FP instructions have higher latency, and you waste time inDouble
⇄UInt64
conversions).