1. ## Recursion programme

Is anyone able to code this?
ive done a sample already look at it:

1 1 2 0 3 -1 4 -2 5....

such that the 3 terms are 1, 1 and 2 and each term thereafter is defined recusively as the sum of the first 2 items minus the third, that is:

seq(n) = seq(n-3)+seq(n-2)-seq(n-1)

the programme accepts the number into the vairable n
implement the recursive function that computes the above series, call the function recursiveSeq().

The main() program should look like this:
cout<< "Enter n = ";
cin>>n;
cout<<"Recursive computation: " <<recursiveSeq(n)<<endl;

Code:
```#include <iostream>
: using namespace std;
:
:
:
: int recursiveSeq(const int n)
: {
: if (1==n || 2==n)
: {
: return 1;
: }
: else if (3==n)
: {
: return 2;
: }
: else
: {
: return recursiveSeq(n-3) + recursiveSeq(n-2) - recursiveSeq(n-1);
: }
: }
:
: using std::cout;
: using std::cin;
: int main()
: {
: int n;
:
: cout<< "Enter n = ";
: cin>>n;
cout<<"Recursive computation: " <<recursiveSeq(n)<<endl;
}```
But it only works for up to some certain of numbers??i dont know why??and how do i do it if i want to input negative numbers??

2. Originally Posted by kenneth_888
But it only works for up to some certain of numbers??
Could you define "doesn't work"? If it appears to be hanging, it could be because you specified a large sequence for it to calculation and it's just taking that long.

Originally Posted by kenneth_888
and how do i do it if i want to input negative numbers??
The important thing is to get your base cases right. You have the program stop calculating a particular entry if n is 1, 2, or 3.

If someone enters -1, then what? When do you want them to stop? You have to figure out when the limit is there to stop them.

3. well i would to be able to input to 10000

4. That's not realistic. Recursive function calls take time, and what's more is that when you call the same function more than once, you're doing it exponentially.

One call with n set to 10,000 = 1 original call + 3 more calls + 9 more calls + 27 more calls + 81 more calls.... etc. etc.. You'll be calling this function many, many times.

The program will run for awhile. If you really need to calculate it faster, then you need to find a faster method of calculating the sequence.

5. ohh i c thanxs, what about with negative numbers??its not possible right
i guess theres nothing wrong with my programme unless i input a big integer.

6. What about negative numbers? What would the formula be for them?

If I enter -1, what do you want the answer to be?