Skip to content

Instantly share code, notes, and snippets.

@danielomiya
Created August 20, 2021 23:52
Show Gist options
  • Select an option

  • Save danielomiya/2a1c3482c9d2cc18e286549119979b67 to your computer and use it in GitHub Desktop.

Select an option

Save danielomiya/2a1c3482c9d2cc18e286549119979b67 to your computer and use it in GitHub Desktop.
A Fibonacci sequence algorithm using Dynamic programming written in C.
#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