Re: Fibonacci implementation



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)
.



Relevant Pages

  • Re: Fibonacci implementation
    ... Christian Christmann wrote: ... pseudo code ... static unsigned long fibo(unsigned int n) ... unsigned long pprev, prev, value; ...
    (comp.programming)
  • Re: Data flow analysis
    ... On Sun, 14 Jan 2007, Christian Christmann wrote: ... int function// 1 ... or a value previously written into 'x' by the assignment in line 3.) ...
    (comp.programming)
  • Re: Strange function use
    ... Christian Christmann wrote: ... int a, b, foo; ... int a, b, foo(int); ... foooccurs inside a declaration. ...
    (comp.lang.c)
  • Re: Function return type
    ... Christian Christmann wrote: ... C89 defines: main ... C99 defines: int main ... Best is: int main ...
    (comp.lang.c)
  • Re: Implicit declaration
    ... Christian Christmann said: ... int main ... When I compile this file, ... void test(int); ...
    (comp.lang.c)