-
Notifications
You must be signed in to change notification settings - Fork 8
/
fibonacci.c
39 lines (33 loc) · 1.04 KB
/
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
33
34
35
36
37
38
39
/*
* Author: Leandro Augusto Lacerda Campos <[email protected]>
*
* Data Structures and Algorithms Specialization,
* by University of California, San Diego,
* and National Research University Higher School of Economics
*
* Course 1: Algorithmic Toolbox
*
* Solution for Fibonacci Number Problem
*/
#include <stdio.h>
#define FIB_TABLE_SIZE 45 + 1 // fib[0], ..., fib[fib_table_p], where fib_table_p <= 45
static unsigned int fib_table[FIB_TABLE_SIZE] = {0, 1}; // { 0, 1, 0, ..., 0 }
static unsigned char fib_table_p = 1; // index to the larger Fibonacci number calculated
unsigned int fibonacci(unsigned char);
int main()
{
unsigned char n;
scanf("%hhu", &n);
printf("%u\n", fibonacci(n));
return 0;
}
// fibonacci: calculate the nth Fibonacci number (assuming that 0 <= n <= 45)
unsigned int fibonacci(unsigned char n)
{
while (fib_table_p < n)
{
fib_table_p++;
fib_table[fib_table_p] = fib_table[fib_table_p - 1] + fib_table[fib_table_p - 2];
}
return fib_table[n];
}