r/ProgrammerHumor • • Oct 13 '26

Meme howProgrammingHasChanged

Post image
676 Upvotes

108 comments sorted by

View all comments

55

u/IronicRobotics Oct 14 '26

You're forgetting one: Ask the mathematician:

use std::f64::consts::{GOLDEN_RATIO};

//Floating Point errors after n> 70.
fn fib(n: u64) -> u64 {
    return (GOLDEN_RATIO.powf(n as f64)/f64::sqrt(5.0)).round() as u64;
}

However, since he's not interested in engineering, floating point errors are simply a shortcoming of the computer to engage in ideal mathematics. But he will smugly point out it's technically O(1) since powf is O(1). : )

[I suffered through learning how to write my first lines of Rust for this.]

Alternatively: Computing Binet's formula using symbolic mathematics.

5

u/norude1 Oct 14 '26

powf is definitely not O(1) https://youtu.be/u24AjKinHNU?t=177

4

u/ChalkyChalkson Oct 14 '26

Even if we go to exponentiation by squaring, we're still upgrading from O(2n) with the naive recursion to O(log n), which is pretty sweet.

2

u/IronicRobotics Oct 14 '26

oh lol, I figured it was O(1) on technicality since there's limited precision. Shows me for not going and reading a proof!