We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 60f651b commit 98d3e24Copy full SHA for 98d3e24
dynamicprogramming/fibonacci_dp.c
@@ -0,0 +1,29 @@
1
+#include <assert.h>
2
+
3
+/**
4
+ * Get nth fibonacci number.
5
+ * @param nth the nth
6
+ * @return nth fibonacci number.
7
+ */
8
+int fibonacci(int nth) {
9
+ int fibs[nth + 1];
10
+ fibs[0] = 0;
11
+ fibs[1] = 1;
12
+ for (int i = 2; i <= nth; ++i) {
13
+ fibs[i] = fibs[i - 1] + fibs[i - 2];
14
+ }
15
+ return fibs[nth];
16
+}
17
18
+void test() {
19
+ assert(0 == fibonacci(0));
20
+ assert(1 == fibonacci(1));
21
+ assert(1 == fibonacci(2));
22
+ assert(34 == fibonacci(9));
23
24
25
+int main() {
26
+ test();
27
+ return 0;
28
29
0 commit comments