Recursion problem

Joined
Aug 11, 2006
Messages
3
Reaction score
0
Hey everyone,

I'm working on an algorithm class now, and I'm trying to understand recursion better. I have a problem that that requires writing a recursive function for a problem using dynamic programming. I have come up with a standard non recursive function, to try and understand the problem better in my mind before converting this to recursion with dynamic programming. However I would like to know if my function is correct for this problem.

Pn = Pn-1 + 2Pn-2, P1 = P0 = 1

int P(int n)
{
int value = 0;

for (int a = n; a >= 2; --a)
{ value += (a - 1); }

for (int b = n; b >= 2; b = b - 2)
{ value += 2 * (b - 2); }

value += 2; // for P1 = P0 = 1

return value;
}

Am I correct on this? Thanks for any and all help!

Firekhan
 
Joined
Aug 11, 2006
Messages
4
Reaction score
0
Don't you need parenthases around the arguments for your function 'P'?

And, I don't understand the first line. You haven't defined P(int n) yet. Maybe I'm not understandnig something.

These are just my thoughts. Maybe this helps at all?
Good luck.
 
Joined
Aug 11, 2006
Messages
3
Reaction score
0
What do you mean defined P(int n)? It's a C++ function.

Pn = Pn-1 + 2Pn-2, P1 = P0 = 1

is a recursive statement, not a function. I'm working on algorithm analysis.
 
Joined
Jul 30, 2006
Messages
5
Reaction score
0
FireKhan said:
What do you mean defined P(int n)? It's a C++ function.

Pn = Pn-1 + 2Pn-2, P1 = P0 = 1

is a recursive statement, not a function. I'm working on algorithm analysis.
Specify a question.

In your example:
Pn = -3/2; P1=P0=1;
And
Code:
double P(int n) {
 if ((n=0)||(n=1)) return 1;
 else return -1.5;
};
 
Joined
Jul 4, 2006
Messages
11
Reaction score
0
FireKhan ....

I agree with the code above . If you have perfect non recursive funtion for your problem. What is the your algo all about ? And do you just want to resolve the problem using recursive method or trying to understand.
Anyway I dont see any questions your post.Good Luck !!
 
Joined
Aug 11, 2006
Messages
3
Reaction score
0
Pavan,
Thanks for the reply. You answered my first question, and yes, I do want to resolve it using a recursive method. However in my own mind I couldn't come up with a recursive method before I saw a loop work with it properly, which is why I wanted to know if my loop was correct. The problem is supposed to be solved with a recursive function using dynamic programming. Thanks for any help!
 
Joined
Jul 30, 2006
Messages
5
Reaction score
0
Recursion should look as follows (schematically):
Code:
double P (int n) {
 double Result;
 <a_necessary_code>
 if (<some_condition>) Result+=P (<some_parameter>);
 <a_necessary_code>
 return Result;
}
 
Joined
Sep 8, 2006
Messages
3
Reaction score
0
quiet right I think..
Code:
if( (1==n)||(0==n) ) return 1;
But what is the need of (0==n).....

As per my understanding the code will return when n becomes 1 and hence n==0 will never be true.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top