Tumble Confused Device

.oOo.

Fibonnaci curiosity

while playing with code that generates Fibonacci sequences, I came about a curiosity. In the following code:

#include <stdio.h>

int fibonacci(int n)
{
  if (n < 2)
  {
    return n;
  }
  return fibonacci(n - 1) + fibonacci(n - 2);
}

int main()
{
  printf("Fibonacci 44 %d\n", fibonacci(44));
  return 0;
}

the recursion return adds the -1 and -2 Fibonacci elements. But you can swap the order of the summation and the result doesn’t change. What is interesting is that if you compile this and run both versions, this one is slightly faster (in my MB Air M2). So… there’s some magical caching behind the scenes.

.oOo.