Last active
August 29, 2015 14:01
-
-
Save Xophmeister/e745014ded101858d6c7 to your computer and use it in GitHub Desktop.
Functional(ish) Fibonacci in C :)
This file contains 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> | |
int (*fn[2])(int); | |
int fib(int n) { return (*fn[n < 2])(n); } | |
int fibSeed(int n) { return 1; } | |
int fibRecur(int n) { return fib(n - 1) + fib(n - 2); } | |
void fibInit(void) { | |
fn[0] = fibRecur; | |
fn[1] = fibSeed; | |
} | |
int main() { | |
int i; | |
fibInit(); | |
for (i = 0; i <= 25; ++i) { | |
printf("%2d %6d\n", i, fib(i)); | |
}; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hooray for function pointers!