954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Order of a recursive function

Hi all,

Can anybody tell me how to calculate the order of a recursive function.
Lets take factorial function.

Without using recursion

int x =1;
for(int i = 1;i<=n;i++)
x = x*i;

In this case the order will the O(n) in terms of time and in terms of space it will be O(1).
Assuming I am right. Correct me otherwise.

Now suppose I implemet the same factorial with rtecursion

return(n==0? 1 : n * fact(n-1));

What will be the order in time and space????

dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
 

Well for a value of 'n', how many recursive calls will there be?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

There will be n recursive calls, so the order will be O(n ) for time complexity. Am i right?
In that case space complexity will also be of O(n)

dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
 

That's right.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

i need help please...i ask, to make a program that accepts a positive integers and displays all odd number between 1 to the integer entered (inclusive) accept the integer in main() and use a recursive function to identify and display the odd numbers.

tnx..salie

salie_90
Newbie Poster
2 posts since Jun 2009
Reputation Points: 8
Solved Threads: 0
 
i need help please...i ask, to make a program that accepts a positive integers and displays all odd number between 1 to the integer entered (inclusive) accept the integer in main() and use a recursive function to identify and display the odd numbers.


What have you done by now?
We can help you, but I assume, that no one writes the hole program for you. So try by yourself und ask about concrete problems.

Deque
Newbie Poster
5 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

i need help please...i ask, to make a program that accepts a positive integers and displays all odd number between 1 to the integer entered (inclusive) accept the integer in main() and use a recursive function to identify and display the odd numbers.

tnx..salie


Don't resurrect old threads to just ask your question, you've already your own thread here: http://www.daniweb.com/forums/post906282.html .

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You