Skip to content

Commit 816c274

Browse files
authored
Create FibonacciMemoization.c
1 parent 513070d commit 816c274

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/c/FibonacciMemoization.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
3+
#define MAX_N 100 // Define the maximum number for which you want to calculate Fibonacci
4+
5+
int memo[MAX_N]; // Memoization table to store computed values
6+
7+
int fibonacci_memoization(int number) {
8+
if (number <= 1) {
9+
return number;
10+
}
11+
if (memo[number] != -1) {
12+
return memo[number];
13+
}
14+
15+
memo[number] = fibonacci_memoization(number - 1) + fibonacci_memoization(number - 2);
16+
return memo[number];
17+
}
18+
19+
int main(void) {
20+
int test_nbr = 12;
21+
22+
// Initialize the memoization table with -1 (uncomputed)
23+
for (int i = 0; i < MAX_N; i++) {
24+
memo[i] = -1;
25+
}
26+
27+
printf("memoization: %d\n", fibonacci_memoization(test_nbr));
28+
return 0;
29+
}

0 commit comments

Comments
 (0)