Re: Fibonacci implementation
- From: Richard Heathfield <invalid@xxxxxxxxxxxxxxx>
- Date: Wed, 30 Aug 2006 10:49:31 +0000
Christian Christmann said:
Hi,
I'm looking for a non-recursive implementation of the algorithm to
calculate Fibonacci numbers. Any language is OK (C/C++, pseudo code
prefered).
Any hints?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char **argv)
{
unsigned long N = (argc > 1 ? strtoul(argv[1], NULL, 10) : 42);
double n = N > 0 ? N : 1;
double n2 = n * n;
double n3 = n2 * n;
double n4 = n2 * n2;
double e = exp(1);
double pi = atan(1) * 4;
double f1 = sqrt(2 * pi * n);
double f2 = pow(n / e, n);
double t1 = 1;
double t2 = 1 / (12 * n);
double t3 = 1 / (288 * n2);
double t4 = 139 / (51840.0 * n3);
double t5 = 571 / (2488320.0 * n4);
double f3 = t1 + t2 + t3 - t4 - t5;
double nbang = f1 * f2 * f3;
printf("%lu! = %.0f\n", N, nbang);
return 0;
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.
- References:
- Fibonacci implementation
- From: Christian Christmann
- Fibonacci implementation
- Prev by Date: Re: break/continue in loops
- Next by Date: Re: Network buffering question
- Previous by thread: Re: Fibonacci implementation
- Next by thread: Re: Fibonacci implementation
- Index(es):
Relevant Pages
|