By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Fibonnaci

P: n/a
Im trying to find any code in C developed to calculate the fibonnacci
series with or without functions. I need a code example so I can
studied and see how it works.

Dec 9 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 2005-12-09, MARQUITOS51 <cr********@gmail.com> wrote:
Im trying to find any code in C developed to calculate the fibonnacci
series with or without functions. I need a code example so I can
studied and see how it works.


If you're cheating on homework, ask for something less obvious than code
examples.

#include <math.h>

double fibon(double n) {
return (
pow(1.6180339887498948482045868343656381177203L,n)
-
pow(-.6180339887498948482045868343656381177203L,n)
)/2.2360679774997896964091736687312762354406L;
}

Good luck explaining to your instructor how this works.

[If you're not, you'll need to give people more to work with to show
that you understand the problem and also give your best effort and show
what code you've tried that doesn't work]
Dec 9 '05 #2

P: n/a
MARQUITOS51 said:
Im trying to find any code in C developed to calculate the fibonnacci
series with or without functions. I need a code example so I can
studied and see how it works.


The Fibonacci series can be defined as f(0) = 0, f(1) = 1, and f(n) = f(n -
1) + f(n - 2) for n > 1.

So it goes like this: 0, 1, 1, 2, 3, 5, 8, 13, etc.

Think about how you would implement this recursively. Hint: look at the
above definition.

Write it. See how slow it is? Find out why. Hint: use printf.

Now think about how you could make it a lot faster. Hint: think about
arrays.

Now see if you can avoid the need for an array by writing this iteratively
instead of recursively.

--
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)
Dec 9 '05 #3

P: n/a

Richard Heathfield wrote:
MARQUITOS51 said:
Im trying to find any code in C developed to calculate the fibonnacci
series with or without functions. I need a code example so I can
studied and see how it works.

[...] Now see if you can avoid the need for an array by writing this iteratively
instead of recursively.


Do you mean Q-Matrix? If not, that's one more thing the OP might want
to check
out.

Dec 9 '05 #4

P: n/a
On 2005-12-09, Richard Heathfield <in*****@invalid.invalid> wrote:
Write it. See how slow it is? Find out why. Hint: use printf.

Now think about how you could make it a lot faster. Hint: think about
arrays.


All that's needed to change the worst-case performance to linear is a
two-element cache.
Dec 9 '05 #5

P: n/a
"Jordan Abel" <jm****@purdue.edu> wrote in message
news:sl********************@random.yi.org...
On 2005-12-09, Richard Heathfield <in*****@invalid.invalid> wrote:
Write it. See how slow it is? Find out why. Hint: use printf.

Now think about how you could make it a lot faster. Hint: think about
arrays.


All that's needed to change the worst-case performance to linear is a
two-element cache.


I think Richard tryed to explain the process... how to move from a top down,
recursion-based solution to a memoized recursion, and then to a bottom-up
solution (building that same array that was used for memoization from 0 to
n). This is infact the best you can do if you need to keep all of the first
n Fibonacci numbers - it is unclear if the OP needs this, or he just wants
to write them out. Also, your comment is covered in the last "idea" Richard
gives.
Dec 9 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.