![]() |
| ||
| fibonacci Hey guys, I'm supposed to write a program that will calculate the Nth fibonacci number, without using recursion. I have the recursion problem written already, but am having a hard time doing the iterative one. here's my recursion function: int fib ( int n ) any help with getting me started on the iterative one? |
| ||
| Re: fibonacci Nevermind, I got it working. Didn't think to use an array; well I thought about it, but didn't think it would make the problem easier. Here's my fibonacci sequence, written iteratively. #include <iostream> |
| ||
| Re: fibonacci Your array based solution is limiting in that you have set the array size to the number of values to display. What about a purely iterative solution, that does not need the excess memory, and is not logically limited in the number of values to display? #include <iostream> |
| ||
| Re: fibonacci >What about a purely iterative solution, that does not need the excess >memory, and is not logically limited in the number of values to display? That's fine for just printing the sequence. The array is more open to extension though. Let's say you want to write fib(x) where x is the number and the function returns int fib ( int x )At the (minor, in this case) cost of extra storage, you avoid recalculating parts of the sequence that you've already calculated. That's the principle behind the dynamic recursive algorithm. |
| ||
| Re: fibonacci seeing as this is being discussed still, I would like to pose another question: How do I use an array using recursion? This will be more efficient, no? Here is my current code: #include <iostream> |
| ||
| Re: fibonacci Quote:
|
| ||
| Re: fibonacci That's a compiler dependent problem now. Previously, any declaration of a local array had to be done with a constant expression as the size. The latest C standard (C99) has changed that restriction, but not all compilers have adopted that. And remember, the C++ standard includes the C standard, more or less. So, legally speaking: int size = 5;is now a legitimate construct. Current version of gcc supports this. Visual C++ does not. Now I have to go change my lesson plans..... Val |
| ||
| Re: fibonacci Ok, I got it to work using a pointer. Here's my code. I can't get it to work with numbers >=12; it gives a system error that it failed unexpectedly. I'm guessing it's a stack issue, but is there a way around it? Is my program the best it can be (using recursion and arrays)?#include <ctime> |
| ||
| Re: fibonacci >And remember, the C++ standard includes the C standard, more or less. Standard C++ currently maintains compatibility with C89, not C99. So you can't use VLAs in C++ >Is my program the best it can be (using recursion and arrays)? No. The entire point of using an array with recursion is to avoid recalculating everything all the time. This is especially true with the Fibonacci sequence, where a lot of the numbers have to be recalculated. You use an array, but you don't take advantage of it, so your function is just a slower, more wasteful version of the original recursive algorithm. What you need is for the array to persist between function calls and be able to tell if a number has been calculated or not. If it has, you don't need to go through the recursive chain. This is where you'll see performance improvements: int fib ( int n ) |
| ||
| Re: fibonacci > I can't get it to work with numbers >=12; it gives a system error that it failed unexpectedly. 1. You don't free the memory you allocate. Eventually, you'll run out of memory. 2. You run off the end of the memory allocated. for ( int i = 2 ; i <= n ; i++ )If you want <= n, then you need to allocate n+1 items. It was always wrong, you just got caught with your hand in the cookie jar at n >= 12. 3. Unlike Ptolemy's answer, your array serves no purpose. |
| ||
| Re: fibonacci Oh, ok. so changing the array to static solved everything. It's still really slow, compared to this one: #include <ctime>But that was expected; we get extra credit for programming both and timing the two. I'm still frustrated that I can't do static int fibArray [ n ] instead of 25, or some other constant value. |
| ||
| Re: fibonacci >so changing the array to static solved everything. No, changing the array to static and actually using the values you've cached solved everything. If you don't to the latter, your code won't be any faster than the usual recursive solution. >It's still really slow, compared to this one Your function might be slow, but mine is a hair faster in my tests than the code you're referring to. I've included a little test below. >I'm still frustrated that I can't do static int fibArray [ n ] >instead of 25, or some other constant value. A better alternative is to pass a buffer to the function rather than use a static or global array. I've made that change in the test below. #include <ctime> |
| ||
| Re: fibonacci Oh ok great. thanks for the help! It's working great! |
| All times are GMT -4. The time now is 5:12 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC