Velocity Reviews > FIbonacci

FIbonacci

MARQUITOS51
Guest
Posts: n/a

 12-11-2005
Hey guys this is the fibonacci series. But Im having this huge problem.
First of all I want to do the simplest code possible so then I can use
user defined functions and arrays. But this one didnt came out well.
Any suggestions!

#include<stdio.h>
#include<math.h>

int main ()

{
int fib, n;

for(n=0; n<=10; n++)
{

fib=(n-1)+(n-2);
printf("%d \n", fib);

}

return 0;
}

Stefan Wallentowitz
Guest
Posts: n/a

 12-11-2005
Am Sun, 11 Dec 2005 07:06:00 -0800 schrieb MARQUITOS51:

> Hey guys this is the fibonacci series. But Im having this huge problem.
> First of all I want to do the simplest code possible so then I can use
> user defined functions and arrays. But this one didnt came out well.
> Any suggestions!
>
>
> #include<stdio.h>
> #include<math.h>
>
> int main ()
>
> {
> int fib, n;
>
> for(n=0; n<=10; n++)
> {
>
> fib=(n-1)+(n-2);
> printf("%d \n", fib);
>
> }
>
> return 0;
> }

As far as I remember, this isn't the fibonacci algorithm.
First it starts with 1 and 1, the third element of fibonacci series is the
addition of the first and second. So fib(n)=fib(n-1)+fib(n-2).
Not saving all elements you are able to only save the last elements in
variables and shift them. But what you did on the indexing element doesn't
make any sense.

Bye,
Stefan

MARQUITOS51
Guest
Posts: n/a

 12-11-2005
Hey I did this a few moments ago and it workes. Can you check it and
suggest how to optimize. Im also looking how to convert it to user
defined function.

#include<stdio.h>
#include<math.h>

int main ()

{ int f[50];
int n=2;

f[0]=0;
f[1]=1;

do
{
f[n]=f[n-1]+f[n-2];
n++;
}
while(n<=49);

for(n=0; n<=49; n++)
{printf("%d \n", f[n]);}

return 0;

}

Christopher Benson-Manica
Guest
Posts: n/a

 12-11-2005
MARQUITOS51 <> wrote:

> Hey I did this a few moments ago and it workes. Can you check it and
> suggest how to optimize. Im also looking how to convert it to user
> defined function.

Sure, it works, if you don't ever plan on wanting more than 50
Fibonacci numbers. malloc() and friends can help you store an
arbitrary number of Fibonacci numbers. If you're just trying to print
them, you don't need more than three distinct variables. Think about
it.

> #include<stdio.h>
> #include<math.h>

Why are you including this header? You're not using anything from it.

> int main ()

int main( void ) /* better */

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Peteris Krumins
Guest
Posts: n/a

 12-11-2005
MARQUITOS51 wrote:
> Hey guys this is the fibonacci series. But Im having this huge problem.
> First of all I want to do the simplest code possible so then I can use
> user defined functions and arrays. But this one didnt came out well.
> Any suggestions!
>

#include <stdio.h>

unsigned fib(unsigned n) {
return n <= 2 ? 1 : fib(n - 1) + fib(n - 2);
}

int main(void) { printf("fib(17) is: %d\n", fib(17)); return 0; }

P.Krumins

Alex Fraser
Guest
Posts: n/a

 12-11-2005
"Peteris Krumins" <> wrote in message
news: ups.com...
[snip]
> #include <stdio.h>
>
> unsigned fib(unsigned n) {
> return n <= 2 ? 1 : fib(n - 1) + fib(n - 2);

Personally, I'd make that:

return n < 2 ? n : fib(n - 2) + fib(n - 1);

> }
>
> int main(void) { printf("fib(17) is: %d\n", fib(17)); return 0; }

Alex

Eric Sosman
Guest
Posts: n/a

 12-11-2005
Peteris Krumins wrote:

> MARQUITOS51 wrote:
>
>>Hey guys this is the fibonacci series. But Im having this huge problem.
>>First of all I want to do the simplest code possible so then I can use
>>user defined functions and arrays. But this one didnt came out well.
>>Any suggestions!
>>

>
>
> #include <stdio.h>
>
> unsigned fib(unsigned n) {
> return n <= 2 ? 1 : fib(n - 1) + fib(n - 2);
> }
>
> int main(void) { printf("fib(17) is: %d\n", fib(17)); return 0; }

Somebody always proposes this solution, but it's a
poor one. Try it with fib(47), say, and tell us how
long it takes. Hint: You won't need a high-precision
timer.

--
Eric Sosman
lid

Peteris Krumins
Guest
Posts: n/a

 12-11-2005
Eric Sosman wrote:
> Peteris Krumins wrote:
>
>
> Somebody always proposes this solution, but it's a
> poor one. Try it with fib(47), say, and tell us how
> long it takes. Hint: You won't need a high-precision
> timer.
>

Yes, the solution has exponential complexity.

P.Krumins

Michael Wojcik
Guest
Posts: n/a

 12-13-2005

In article <z7adnct6kL7RAwHeRVn->, Eric Sosman <> writes:
> Peteris Krumins wrote:
> >
> > #include <stdio.h>
> >
> > unsigned fib(unsigned n) {
> > return n <= 2 ? 1 : fib(n - 1) + fib(n - 2);
> > }
> >
> > int main(void) { printf("fib(17) is: %d\n", fib(17)); return 0; }

>
> Somebody always proposes this solution, but it's a
> poor one. Try it with fib(47), say, and tell us how
> long it takes. Hint: You won't need a high-precision
> timer.

Well, there's always this one (with error checking left as an
exercise for the reader):

/***
prev2 and prev are the two immediately preceeding values in the
series; prev2 is F(n-2) and prev is F(n).
pos is the current position in the series.
want is the position we're looking for.
***/

unsigned fib_cps(unsigned prev2, unsigned prev, unsigned pos, unsigned want)
{
if (want <= 2) return 1;
if (pos == want) return prev2 + prev;
return fib_r(prev, prev2 + prev, pos + 1, want);
}

unsigned fib(n)
{
return fib_cps(1, 1, 3, n);
}

Even written this way, and compiled without optimization (I verified
that the compiler left the tail-recursive call in rather than
optimizing it to a branch), this computes fib(47) in negligible time.
But of course it's just the forward-iterative version written as tail
recursion.

It'd be possible to rewrite Peteris' backward-recursing algorithm
using continuation-passing style and tail recursion, but since C
lacks dynamic closures we'd have to emulate them. And that would
just involve recursively creating some sort of list of instructions
to run the forward-iterative algorithm, so it's not very interesting.

--
Michael Wojcik

Is it any wonder the world's gone insane, with information come to be
the only real medium of exchange? -- Thomas Pynchon

websnarf@gmail.com
Guest
Posts: n/a

 12-14-2005
Peteris Krumins wrote:
> Eric Sosman wrote:
> > Peteris Krumins wrote:
> > Somebody always proposes this solution, but it's a
> > poor one. Try it with fib(47), say, and tell us how
> > long it takes. Hint: You won't need a high-precision
> > timer.
> >

>
> Yes, the solution has exponential complexity.

Exercise to the reader: Describe a function to precisely count the
number of "+" operations performed by this function for computing f(n).
(Hint: its easier than you think.)

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/