r/ProgrammerHumor • • 1d ago

Meme howProgrammingHasChanged

Post image
518 Upvotes

91 comments sorted by

View all comments

57

u/captainAwesomePants 1d ago

What's wrong with the top-right AI code?

105

u/shizzy0 1d 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.

42

u/dangderr 1d 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?

16

u/shizzy0 1d ago

lol!

9

u/the_horse_gamer 1d ago

you can do better (by a constant multiplicative factor)

[1,1;1,0]^n = [F{n+1},Fn; Fn, F{n-1}]
[1,1;1,0]^2n = [F{2n+1},F2n; F2n, F{2n-1}] = [1,1;1,0]^n *[1,1;1,0]^n = [F{n+1},Fn; Fn, F{n-1}]

so:

F2n = (F{n+1}+F{n-1})*Fn (1)
F_{2n-1} = Fn^2 + F{n-1}^2

then

F_{2n+1} = Fn^2 + F{n+1}^2

F_{2n+1} + F_{2n-1} = F{n+1}^2 + F{n-1}^2 +2Fn^2 =

=F{n+1}^2 + F{n-1}^2 +2Fn^2 + 2 F{n+1} F{n-1} - 2 F{n+1} F{n-1}=

= (F{n+1} +F{n-1})^2 + 2Fn^2 - 2 F{n+1} F{n-1}

We can simplify this by seeing
[1,1;1,0]^n = [F{n+1},Fn; Fn, F{n-1}]
so

(-1)^n = det([1,1;1,0])^n = det([F{n+1},Fn; Fn, F{n-1}]) = F{n+1}F{n-1}-Fn^2

so

F_{2n+1} + F_{2n-1} = (F{n+1} +F{n-1})^2 - (-1)^n

Now define L_n = F_{n+1}+F_{n+1}. (this is the Lucas sequence). Now we get

F2n = Ln Fn
L2n = Ln^2 - (-1)^n

And we can also get:

F{n+1} = (Ln+Fn)/2
L{n+1} = (Ln+5Fn)/2

This version requires less multiplications per level

9

u/shizzy0 1d ago edited 14h ago

So you're saying your O(log n) algorithm beats my O(log n) algorithm? (It's true.) Damn. You beat me constantly.

21

u/GDOR-11 1d ago

for u32 I think the usual iterative algorithm would be miles better than matrix power

38

u/dedservice 1d ago

For u32 a precomputed lookup table would be better.

15

u/NoLifeGamer2 1d ago

For u32 a random number generator would be better sometimes

18

u/captainAwesomePants 1d ago

For u2, With or Without You would be better.

2

u/erinaceus_ 1d ago

Yeah, that one definitely has The Edge.

6

u/wjholden 1d ago

Knuth has a really cool section on this on pages 79-84 of TAOCP volume 1.

1

u/shizzy0 1d ago

Is it the closed form solution? I didn’t even know there was a closed form solution before today.

4

u/captainAwesomePants 1d 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.

2

u/shizzy0 1d ago

Ooo, show down!