Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > FIbonacci

Reply
Thread Tools

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;
}

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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;


}

 
Reply With Quote
 
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.
 
Reply With Quote
 
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

 
Reply With Quote
 
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


 
Reply With Quote
 
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
 
Reply With Quote
 
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

 
Reply With Quote
 
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
 
Reply With Quote
 
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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fibonacci numbers dleecurt@cc.usu.edu C++ 28 06-05-2011 04:30 AM
Fibonacci problem Brett Trost Perl 2 01-22-2004 02:04 PM
STL for Fibonacci Heap?? Lance C++ 5 12-02-2003 09:17 AM
Computing Fibonacci numbers as a performance testsuite Alex Vinokur C++ 0 10-29-2003 12:07 PM
How to do fibonacci by linked list ( not by recursive) fighterman19 C++ 11 09-08-2003 02:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57