Iterative Fibonacci in C
Fibonacci without recursion, two variables and a loop.
unsigned long fib(unsigned n) {
unsigned long a = 0, b = 1;
while (n--) {
unsigned long next = a + b;
a = b;
b = next;
}
return a;
}
Keywords and builtins used here
fiblongreturnunsignedwhile
The run, in numbers
- Lines
- 9
- Characters to type
- 132
- Tokens
- 46
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 31 seconds.
Step 5 of 7 in Functions, step 19 of 35 in Language basics.