Created
August 20, 2021 23:52
-
-
Save danielomiya/2a1c3482c9d2cc18e286549119979b67 to your computer and use it in GitHub Desktop.
A Fibonacci sequence algorithm using Dynamic programming written in C.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #define N 512 | |
| long long int CACHE[N] = { 0, 1 }; | |
| long long int fib(int n) { | |
| if (n > 0 && !CACHE[n]) | |
| CACHE[n] = fib(n - 2) + fib(n - 1); | |
| return CACHE[n]; | |
| } | |
| int main(int argc, char** argv) { | |
| int n; | |
| if (argc <= 1) { | |
| printf("Usage: %s <N>\n", argv[0]); | |
| return 400; | |
| } | |
| n = atoi(argv[1]); | |
| printf("The #%d term of Fibonacci is %lld\n", n, fib(n)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment