Fibonacci Pattern Using Recursion

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Guest

Fibonacci Pattern Using Recursion

Post by Guest »

As you may know, I am learning C++. After I learn something new, I make a small little program to demonstrate and get the hand of things. After I had read about functions, I decided to make a program demonstrating recursion. I did a Fibonacci pattern, where you input a number to jump to in the sequence and it tells what number is there.

The souce for the dinky little thing is:

Code: Select all

#include <iostream>

using namespace std;

int fib(int n);

int main() {

	int n, answer;

	cout << "\t\t\t Fibonacci Using Recursion\n\n";
	cout << "Enter the number to find: ";
	cin >> n;

	cout << "\n\n";

	answer = fib(n);

	cout <<  answer << " is the " << n << "th Fibonacci number.\n";

	return 0;

}

int fib(int n){

	cout << "Processing fib(" << n << ")...";
	
	if (n < 3) {
		cout << "Return 1!\n";
		return 1;
	}
	else {
		cout << "Call fib(" << n-2 << ") and fib(" << n-1 << ").\n";
		return( fib(n-2) + fib(n-1) );
	}

}
Anyway, it works, which is kinda the point. I started by typing in 6, and it returned 8, and showed every step. Sadly, I inputted 20 next. OMFG! It took 15 minutes to compute!! It went through the function 13,529 times to receive the answer!! DON'T TYPE IN 20 AND RUN IT! I thought I screwed it up, but it did end. lol
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

Not only did you post two of this topic, but you also posted it in the complete wrong forum.

You're really pissing me off with your lack of respect for seperate forums. This is a warning, the next time it happens I'll delete the topic without notice.

Why the hell are you posting pure programming along with source code in PC Dev? Douche bag...
DJ Yoshi
Game Developer
Game Developer
Posts: 1021
Joined: Wed Mar 02, 2005 9:12 pm
Location: Madison, AL

Post by DJ Yoshi »

Dunno what you're looking for here. No comment lines, code is bloated, instead of just taking time to calculate the 20th you make it print out tons of crap it really doesn't need to.
There is no signature.
Guest

Post by Guest »

Seriously, I'd appreciate it if you STFU.

As I ALREADY stated, the object of the program is to demonstrate recursion. If it didn't print all that "crap" then how could I tell what function branched off to what and if it froze or not? Recursion used in such a way is a memory eater, and could easily crash your computer. I could do it a stable, way faster way without "bloated" code, but I chose not to.

Now, I'm going to say this nicely one more time: I don't want you in my programming topics anymore, you're too big a douche. :spin:

None "bloated" code with way too many comments that dosn't eat memory:

Code: Select all

#include <iostream>

// Function prototype(s)
int fib(int position);

int main() {

	int answer,position;
	
	// Displays Question and assigns anser to variable "position"
	std::cout << "Which position in the Fibonacci Sequence would you like to view?\n";
	std::cin >> position;
	std::cout << std::endl;

	//passes position to the fib function to be solved.
	answer = fib(position);
	
	// displays the answer
	std::cout << answer << " is the  ";
	std::cout << position << "th Fibonacci number.\n";
	
	// returns 0 so windows or some other program won't detect an error.
	return 0;
}

//Finds the passed position in the Fibonacci sequence

int fib(int n) {

	int minusTwo=1, minusOne=1, answer = 2;
	
	//checks to see if you inputted something less than 3, if so the anser is 1, so return it.
	if (n < 3) 
		return 1;
	
	//go through sequence until position is reached, then return value at corresponding position.
	for (n -= 3; n; n--) {
		minusTwo = minusOne;
		minusOne = answer;
		answer = minusOne + minusTwo;
	}
	
	//Return the answer
	return answer;

}
DJ Yoshi
Game Developer
Game Developer
Posts: 1021
Joined: Wed Mar 02, 2005 9:12 pm
Location: Madison, AL

Post by DJ Yoshi »

Well one way to easily speed it up would be to not post so much crap to 'keep you updated'. Just post the answer. Then do it yourself on a calculator or with good old fashioned brain power to check. If it doesn't check out, THEN add a cout somewhere.

I'd appreciate it if you took some criticism instead of acting like a 12 year old prepubescent little girl.
There is no signature.
Guest

Post by Guest »

Notice the second post that it doesn't display all the crap? And, notice I just turned 13? :roll:

And, the point of doing the program was FUNCTION RECURSION, NOT GETTING THE ANSWER! How hard is that to understand? I DO need it to display the other crap, because that's the whole point of the program. If ANYTHING, I should make it NOT display the answer.
User avatar
vmrob
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Fri Sep 03, 2004 5:13 pm

Post by vmrob »

is there some other way that might be faster to create the fibronocci sequence? not to insult your logic, that would take me a while to write, but is there?
Guest

Post by Guest »

Are you talking about a formula to use to find it? If there is, I dunno and I'm NOT interested in finding out, but I'm sure there is.

Anyway, what do you mean it'll take too long to write? :|
You writing the sequence out or something?
DJ Yoshi
Game Developer
Game Developer
Posts: 1021
Joined: Wed Mar 02, 2005 9:12 pm
Location: Madison, AL

Post by DJ Yoshi »

If you end up with the right answer, guess what, your recursion worked. And you'd have a much more efficient program.
There is no signature.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

Yeah, it can be done using loops. I might make a looping version later tonight just to show you, but as far as the best way, I'd seriously use the recursion.

It'd be all complicated with loops and just plain ugly. The recursion, while ugly and a memory whore, is still the best programming method for this kind of thing.

But then don't forget that recursion is not the answer to most other things. Don't take it for granted as a recursive approach is generally a crappy one.
Guest

Post by Guest »

If you end up with the right answer, guess what, your recursion worked. And you'd have a much more efficient program.
Why is the concept so hard to grasp that I made the program how I wanted it? I don't see how it could be so hard to understand that it is a correctly written program that does what it's supposed to: VISUALLY display recursion. The point of the program as not to answer the Fibonacci sequence, like the second one I posted was designed to do, but it was made to do exactly what it does.

Gyrovorbis, there is no need to post the source using loops because of the fact that I already did in my third post of this topic.
WeeboHyren
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Fri Feb 04, 2005 5:12 pm
Location: Omega Termini

Post by WeeboHyren »

Snagged this from an old prog I had.

Code: Select all

#include<iostream.h>
void fibb(unsigned long int x,unsigned long int y,unsigned long int a,unsigned long int b){
     if(x==y)cout<<a+b<<"\n";
     else{cout<<a+b<<"\n";
           fibb(x,++y,b,a+b);}}
main()
{
    unsigned long int x;
    system("title Fibbonacci Sequence");
    cin>>x;
    system("cls");
    fibb(x,1,0,1);
    system("PAUSE");
}
:D
101010
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

Wow, nice to see you on the forums. :)

That's nice.
Guest

Post by Guest »

Recursion. There we go. :D
DJ Yoshi
Game Developer
Game Developer
Posts: 1021
Joined: Wed Mar 02, 2005 9:12 pm
Location: Madison, AL

Post by DJ Yoshi »

I love how Weebo drives home my point...and you kind of ignore that fact.

:roll:
There is no signature.
Locked