Savant & CodeResistor
Hey, have you ever wondered how the Fibonacci sequence might line up with cache line boundaries and affect pipeline stalls?
Sure, but only if you can keep it in a single line of code, not a lecture on theory. Cache lines are a low‑level detail, Fibonacci is a math sequence—just hope your compiler's branch predictor doesn’t get jealous.
for(int i=0,a=0,b=1;i<10;i++){int c=a+b;a=b;b=c;printf("%d ",a);}
Looks good, but you’re printing after the first addition, so you’ll miss the zero, and the loop is a bit over‑engineered for ten terms. If you wanted to be fancy, use a const array or something. Happy debugging.
You're right about the missing zero; just start with `a=0; b=1;` then print `a` before the loop, and inside use `printf("%d ", b);` before updating. That keeps it clean and true to the sequence.
Nice tweak. Now the output will start with 0, and you’re not leaking the sequence. If you ever need more than ten terms, just pull that into a function so you don’t rewrite the loop every time.
Sure, just wrap it:
```
void fib(int n){int a=0,b=1;printf("%d ",a);for(int i=1;i<n;i++){printf("%d ",b);int c=a+b;a=b;b=c;}puts("");}
```
Nice, but that `printf` inside the loop forces a syscall each time—worth it for readability, but if you’re chasing performance, buffer the output or use a single `write` later. Also consider passing `n` as size_t and checking for overflow if you grow beyond 93. Keep the loop tight, but that’s your call.
Good point about the syscalls, I’ll buffer into a char array and write once; and yeah, casting to size_t and guarding the loop will keep it safe once we hit the 93‑term limit. Thanks for the heads‑up.
You’re welcome, just make sure the buffer is big enough and you zero‑terminate before the final write. Happy coding.