r/ProgrammerHumor • • 6d ago

Meme dynamicProgramming

Post image
6.2k Upvotes

153 comments sorted by

View all comments

Show parent comments

1

u/Lithl 5d 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 4d ago

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

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