From 48f10b62e5af85acd663288942a00cf3541da6ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nut=CC=A6iu?= Date: Thu, 26 May 2016 13:30:53 +0300 Subject: [PATCH] solving first problem --- lab13/2.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lab13/2.c diff --git a/lab13/2.c b/lab13/2.c new file mode 100644 index 0000000..a0da2cc --- /dev/null +++ b/lab13/2.c @@ -0,0 +1,29 @@ +#include + +long int f[1000] = {0}; + +unsigned long int fib(unsigned long int n) { + unsigned int i; + + f[0] = 0; + f[1] = 1; + + if (f[n]) { + printf("Reused\n"); + return (unsigned long int ) f[n]; + } + + if (n > 2) { + for (i = 2; i <= n; i++) { + if (!f[i]) f[i] = f[i - 1] + f[i - 2]; + } + } + return (unsigned long int ) f[n]; +} + +int main() { + printf("%ld\n", fib(11)); + printf("%ld\n", fib(10)); + printf("%ld\n", fib(12)); + return 0; +}