r/ProgrammerHumor • • 1d ago

Meme dynamicProgramming

Post image
5.3k Upvotes

140 comments sorted by

View all comments

Show parent comment

1

u/Lithl 22h ago
const root5 = Math.sqrt(5);
const phi = (1 + root5) / 2;
const psi = (1 - root5) / 2;

function fib(n) {
    return (Math.pow(phi, n) - Math.pow(psi, n)) / root5;
}

Closed-form Fibonacci expression. O(1) if you ignore that exponentiation isn't constant time.

0

u/donaldhobson 11h ago

And if you ignore that floating point errors cause problems at n=70.

Whereas I was using python, which has arbitrary size ints.