Ad

Return a the number in the nth position of the Fibonacci sequence.

Example:
// Returns 34
get_fibonacci_n(9);

#define PI 3.14159265358979323846
#include <stddef.h>
#include <math.h>

size_t get_fibonacci_n(double n) {
  if (n < 0) return -1;
  double PHI = (1 + sqrt(5)) / 2;
  double a = pow(PHI, n) - pow(-PHI, -n);
  return (size_t)floor(a / sqrt(5));
}