473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Fibonnaci

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
5 1890
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
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

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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bruno R. Dias | last post by:
Perhaps it would be interesting to program a virtual machine simulating an ancient computer (such as the pdp-7). Then, it would be rather interesting to code for it (porting gcc to it maybe?). I...
1
by: Sriram Krishnan [at] gmx [dot] net | last post by:
Given an arbitrary number series, how do I identify the series and find the Nth number... for e.g 1,3,5,7 is an AP and the next number will be 9 1,8,27,64 is the series of cubes and the next will...
5
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h if there is anybody who could help me with this hw i really...
1
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h hi iam done with my hw i still have to do one function which is...
43
by: Tim Chase | last post by:
Just as a pedantic exercise to try and understand Python a bit better, I decided to try to make a generator or class that would allow me to unpack an arbitrary number of calculatible values. In...
5
by: ebrimagillen | last post by:
Hello mates, Just needed a solution on the exercises below. Exercise 2 A classic problem in introductory programming is the grains of rice on a chess board problem. Your program should...
14
by: bearophileHUGS | last post by:
The The Computer Language Shootout has just published results for Python 2.5 and Psyco 1.5.2. Comparing the old (Python 2.4) Gentoo Pentium 4 results (now not visible anymore) with the new results,...
1
by: vaisali | last post by:
fibonnaci series using reccursion pls give me d code in C
16
by: showellshowell | last post by:
I would like to present an unusual take on computing the Fibonacci sequence using Javascript. Please try out the following link in Firefox (sorry, having problems with other browsers): ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.