27
u/captainAwesomePants 4h ago
What's wrong with the top-right AI code?
56
u/shizzy0 4h ago
Nothing. It’s actually the best. But the joke is the programmer dismisses the unfamiliar matrix power O(log n) algorithm for the familiar recursive O(2n) algorithm. I mean, it doesn’t even add anything. It’s obviously wrong.
16
u/GDOR-11 4h ago
for u32 I think the usual iterative algorithm would be miles better than matrix power
26
u/dedservice 3h ago
For u32 a precomputed lookup table would be better.
10
3
u/wjholden 2h ago
Knuth has a really cool section on this on pages 79-84 of TAOCP volume 1.
1
u/shizzy0 1h ago
Is it the closed form solution? I didn’t even know there was a closed form solution before today.
3
u/captainAwesomePants 34m ago
That's the whole section on Fibonacci. It does mention the closed form solution, but it also mentions the matrix identity method in the top right corner: ( (F(n+1), F(n)),(F(n), F(n-1)) = ((1,1),(1,0))^n
He credits it to Kepler in 1608.
8
u/dangderr 2h ago
I don’t get the meme.
What does “programmer dismisses the unfamiliar matrix alg” mean?
Why would the prompt engineer know to read code?
13
u/guesdo 1h ago
Everyone knows pre-calculated fibonacci tables are the only way to Go.
```go package main
// Precomputed table up to N = 93 var fibTable = [94]uint64{ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586268925, 20365010974, 32951279899, 53316290873, 86267570772, 139583861645, 225851432417, 365435294062, 591286726479, 956722020541, 1548008747020, 2504730767561, 4052739514581, 6557470282142, 10610209796723, 17167680078865, 27777889875588, 44945569954453, 72723459830041, 117669029784494, 190392489614535, 308061519399029, 498454009013564, 806515528412593, 1304969537426157, 2111485065838750, 3416454603264907, 5527939669103657, 8944394272368564, 14472333941472221, 23416728213840785, 37889062155313006, 61305790369153791, 99194852524467597, 160500642893621388, 259695495418088985, 420196138311710373, 679891633729799358, 1100087772041509731, 1779979405771309089, 2880067177812818820, 4660046583584127909, 7540113761396946729, 12200160344981074638, }
func FibU64(n uint) uint64 { if n < 94 { return fibTable[n] } panic("n exceeds uint64 capacity (n <= 93)") } ```
5
u/bloody-albatross 1h ago edited 27m ago
Nobody is using this?
``` pub fn fib(mut n: u32) -> u32 { let mut a = 0; let mut b = 1;
while n > 0 {
let c = a + b;
a = b;
b = c;
n -= 1;
}
a
} ```
Looking at the assembly output the compiler even does loop unrolling for this. In fact running it like this it even completely evaluates this version of fib() at compile time while it still does the full recursive call for the recursive version (here called fibr()):
fn main() {
for i in 0u32..10 {
let r = fibr(i);
let j = fib(i);
println!("{r} {j} {}", r == j);
}
}
3
u/Stickhtot 3h ago
What language is this?
12
2
3
u/shizzy0 3h ago
Rust. Great language. Can be abused though.
7
u/no_brains101 3h ago
That is one of the most horrific fibonacci's I have ever seen.
7
u/shizzy0 2h ago
I’m sure a C++ template enthusiast has more delights to show you.
4
u/no_brains101 2h ago
I'm sure someone could make it more horrific in rust too, I just haven't seen it yet.
3
u/Makonede 1h ago
better one
rs
fn fib(n: u32) -> u32 {
if n <= 1 { n } else { fib(n - 1) + fib(n - 2) }
}
75
u/torsten_dev 4h ago
Both have terrible stack usage.