Post: [C++] Fibanocci Term Calculator w/ Recursion
03-14-2011, 01:10 AM #1
Ritztro
I am a Game Developer
(adsbygoogle = window.adsbygoogle || []).push({}); WARNING! When I tried to use this on the number 50, my computers cpu got up to 140 so be carefull with high numbers. (Someone with cooling fans, try running 1,000 and let your computer sit for a liek half an hour or so and see what happens)

Here is my fibanocci series term calculator:
    
#include <iostream>

using namespace std;

long long Fibanocci(int nth);

int main()
{
long long * piNum = new long long;
cout << "Please enter what term you would like from the Fibanocci series (warnign! High numeber (even 50) can overheat your computer): ";
cin >> *piNum;
cout << "\nYour value is: " << Fibanocci(*piNum) << endl;
cin.get();


return 0;
}

long long Fibanocci(int nth)
{
long long temp = 0;
if (nth < 4) return 1;
temp = (Fibanocci(nth-2) + Fibanocci(nth -1));
return temp;
}
03-14-2011, 01:13 AM #2
kiwimoosical
Bounty hunter
Watch out for stack overflow. You never monitor your recurse count.
03-14-2011, 01:32 AM #3
Ritztro
I am a Game Developer
Originally posted by kiwimoosical View Post
Watch out for stack overflow. You never monitor your recurse count.


Should I store the int temp on the heap? and then delete it in the function call? Because then there is no chance of that because it will re-use the memory location correct?
03-14-2011, 02:01 AM #4
What does this do *Sigh* /facepalm *feels stupid*
03-14-2011, 02:15 AM #5
Ritztro
I am a Game Developer
Originally posted by kiwimoosical View Post
Watch out for stack overflow. You never monitor your recurse count.


I can't figure out how to do this so that I always delete the var right after the fib func ends without having memory leaks.. Can you help?

---------- Post added at 07:15 PM ---------- Previous post was at 07:12 PM ----------

Originally posted by TheUberFail View Post
What does this do *Sigh* /facepalm *feels stupid*


Ok the fibonacci sequence goes like this: 1,1,2,3,6,9,14,23, etc. The numbers add on each other. 1+1 = 2, 1+2 = 3. See the pattern With this you can get any term in the sequence.
03-14-2011, 02:28 AM #6
Originally posted by Dutch View Post
I can't figure out how to do this so that I always delete the var right after the fib func ends without having memory leaks.. Can you help?

---------- Post added at 07:15 PM ---------- Previous post was at 07:12 PM ----------



Ok the fibonacci sequence goes like this: 1,1,2,3,6,9,14,23, etc. The numbers add on each other. 1+1 = 2, 1+2 = 3. See the pattern With this you can get any term in the sequence.


would this not be simpler

    
unsigned short fibonacci(unsigned short sequence)
{
unsigned short temp = 1;
unsigned short preTemp = 0;
for(sequence ; sequence > 0 ; sequence --)
{
temp += preTemp;
preTemp = temp;
}

return temp;
}

X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
03-14-2011, 03:13 AM #7
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
would this not be simpler

    
unsigned short fibonacci(unsigned short sequence)
{
unsigned short temp = 1;
unsigned short preTemp = 0;
for(sequence ; sequence > 0 ; sequence --)
{
temp += preTemp;
preTemp = temp;
}

return temp;
}

X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Maybe it would be but I was practicing recursion.
03-14-2011, 12:19 PM #8
Originally posted by Dutch View Post
Maybe it would be but I was practicing recursion.


Hey recursion is pretty cool, i got it working without Happy

Where can recursion be useful btw? it seems to lag the CPU out like crazy xD

    
#include <iostream>

using namespace std;

long long Fibonacci(unsigned short nth);

int main()
{
unsigned short num;
for(;Winky Winky
{
cout<<"Fibonacci: ";
cin>>num;
cout<<"Result: "<<Fibonacci(num -1)<<endl<<endl;
}
return 0;
}

long long Fibonacci(unsigned short n) {
if(n==0)
{
return 0;
}
if(n<4)
{
return 1;
}
long long seq[3]= {1,0,0};
for(short count = 1; count < n; count++)
{
seq[2] = seq[1];
seq[1] = seq[0];
seq[0] = (seq[2] + seq[1]);

}
return seq[0];
}
03-14-2011, 02:38 PM #9
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
Hey recursion is pretty cool, i got it working without Happy

Where can recursion be useful btw? it seems to lag the CPU out like crazy xD

    
#include <iostream>

using namespace std;

long long Fibonacci(unsigned short nth);

int main()
{
unsigned short num;
for(;Winky Winky
{
cout<<"Fibonacci: ";
cin>>num;
cout<<"Result: "<<Fibonacci(num -1)<<endl<<endl;
}
return 0;
}

long long Fibonacci(unsigned short n) {
if(n==0)
{
return 0;
}
if(n<4)
{
return 1;
}
long long seq[3]= {1,0,0};
for(short count = 1; count < n; count++)
{
seq[2] = seq[1];
seq[1] = seq[0];
seq[0] = (seq[2] + seq[1]);

}
return seq[0];
}


Idk when it will be usefull but it will probablly be useful to solve some really hard question.
03-14-2011, 07:06 PM #10
kiwimoosical
Bounty hunter
Originally posted by Dutch View Post
I can't figure out how to do this so that I always delete the var right after the fib func ends without having memory leaks.. Can you help?

---------- Post added at 07:15 PM ---------- Previous post was at 07:12 PM ----------



Ok the fibonacci sequence goes like this: 1,1,2,3,6,9,14,23, etc. The numbers add on each other. 1+1 = 2, 1+2 = 3. See the pattern With this you can get any term in the sequence.


Pass it to the function by reference.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo