next up previous contents index Search
Next: 0.5.8.2 Source Code Up: 0.5.8 Fibonacci Calculation Previous: 0.5.8 Fibonacci Calculation

0.5.8.1 Source Code


/* return the nth term of the fib seq */

int fib3(int n) {
  int i = 1;
  int j = 0;
  int k = 0;
  int h = 1;
  int t = 999;

  while (n > 0) {

    if (n % 2) {
      t = j * h;
      j = i * h + j * k + t;
      i = i * k + t;
    }
    
    t = h * h;
    h = 2 * k * h + t;
    k = k * k + t;
    n = n / 2;
  }

  return(j);
}

Scott Gasch
1999-07-09