Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
fibonacci_fast.c File Reference

Compute \(m^{mth}\) Fibonacci number using the formulae: More...

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for fibonacci_fast.c:

Functions

void fib (unsigned long n, unsigned long *C, unsigned long *D)
 Returns the \(n^{th}\) and \(n+1^{th}\) Fibonacci number. More...
 
int main (int argc, char *argv[])
 main function
 

Detailed Description

Compute \(m^{mth}\) Fibonacci number using the formulae:

Author
Krishna Vedala
Date
2 October, 2019

\begin{eqnarray*} F_{2n-1} &=& F_n^2 + F_{n-1}^2 \\ F_{2n} &=& F_n\left(2F_{n-1} + F_n\right) \end{eqnarray*}

Function Documentation

◆ fib()

void fib ( unsigned long  n,
unsigned long *  C,
unsigned long *  D 
)

Returns the \(n^{th}\) and \(n+1^{th}\) Fibonacci number.

The return variables are C & D respectively.

21 {
22  // Out of Range checking
23  if (n < 0)
24  {
25  printf("\nNo Such term !\n");
26  exit(0);
27  }
28 
29  unsigned long a, b, c, d;
30 
31  if (n == 0)
32  {
33  C[0] = 0;
34  if (D)
35  D[0] = 1;
36  return;
37  }
38 
39  fib(n >> 1, &c, &d); /* Compute F(n/2) */
40 
41  a = c * ((d << 1) - c);
42  b = c * c + d * d;
43  if (n % 2 == 0) /* If n is even */
44  {
45  C[0] = a;
46  if (D)
47  D[0] = b;
48  return;
49  }
50 
51  /**< If n is odd */
52  C[0] = b;
53  if (D)
54  D[0] = a + b;
55  return;
56 }
fib
void fib(unsigned long n, unsigned long *C, unsigned long *D)
Returns the and Fibonacci number.
Definition: fibonacci_fast.c:20