-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.c
32 lines (27 loc) · 827 Bytes
/
fibonacci.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* @author Spencer Melnick
* E-Mail: spencer.melnick@temple.edu
* February 19, 2016
*
* A basic implementation of a recursive Fibonacci number generator in C
*
* NOTE: limited to the 33rd Fibonacci number due to unsigned integer max
* To get later numbers in the series, use unsigned long (or unsigned long
* long in C99 or later)
*/
#include <stdio.h>
unsigned int fib(unsigned int n); //returns the nth number in the Fibonacci sequence
unsigned int fib_recursive(unsigned int, unsigned int, unsigned int);
unsigned int fib(unsigned int n)
{
return ((n == 0) ? 0 : fib_recursive(1, 1, n));
}
unsigned int fib_recursive(unsigned int num1, unsigned int num2, unsigned int n)
{
return ((n < 3) ? num2 : fib_recursive(num2, num1 + num2, --n));
}
int main()
{
printf("%u\n", fib(33));
return 0;
}