dComArk15/Aflevering5-x86-64/fib_iter.c

36 lines
611 B
C
Raw Permalink Normal View History

2016-01-03 12:29:05 +00:00
#include <stdio.h>
#include <stdlib.h>
unsigned long long fib_iter(unsigned long long a)
{
unsigned long long r = 0;
unsigned long long minus_one = 1;
unsigned long long minus_two = 0;
unsigned long long i;
if (a == 0) {
r = 0;
} else if (a == 1) {
r = 1;
} else {
for (i = 2; i <= a; i++) {
r = minus_one + minus_two;
minus_two = minus_one;
minus_one = r;
}
}
return r;
}
int main(int argc, char *argv[])
{
unsigned long long a = 0;
unsigned long long r = 0;
a = atol(argv[1]);
r = fib_iter(a);
printf("fib(%llu) = %llu\n",a,r);
return 0;
}