Need Help on how to write this recursive code

  • Thread starter Thread starter shermaine80
  • Start date Start date
  • Tags Tags
    Code
AI Thread Summary
The discussion centers on writing recursive code for the power function, power(x, n), which calculates x raised to the power of n. It emphasizes the importance of a stopping condition, such as power(x, 1) returning x, and outlines how to handle even and odd exponents recursively. Participants provide examples of recursion using the factorial function to illustrate the concept, explaining the need for both stopping checks and recursive calls to simplify the problem. Additionally, there is a query about using a recursive power function to compute a summation of powers, which leads to further clarification on the stopping condition. Understanding these principles is crucial for effectively implementing recursive functions.
shermaine80
Messages
30
Reaction score
0
write recursive code for the following integer function:

power(x,n) is the exponential or power function x^n, where n is the exponent

For example: power(3,4) evaluates 3^4 which is 3 x 3 x 3 x 3 = 81

Expect the integer result to be large(ie in the millions)

explain how recursive code works.

Please help me how to write this code.
Thanks.
 
Technology news on Phys.org
well recusion is a very simple thing - the whole point is that you will be calling the same function again but:
- the function has a condition at which it will not call itself but return
- by each call the paramter should get closer to the condition.

Example: You can define the Factorial function recursively
btw n!=1*2*...(n-1)*(n)
e.g. 3!=1*2*3=6

Now you can get n! = (n-1)!*n

that is 4! = 3! * 4 = 6*4 = 24

ok, soo to calculate n! we would call (n-1)! and multiply with n. But there must also be a stopping condition. That will be 0! = 1, the condition at which the method should return 1, other it should call itself with (n-1) parameter. Enough talk, the function would look like this

Java code:

public long factorial(long n){

if(n == 0){
return 1;
}else{
return factorial(n-1)*n;
}

}

now if you call factorial(3) what whould happen

factorial(3) calls
factotrial(2) calls
factorial(1) calls
factorial(0) returns 1
factorial(1) returns 1*1=1
factorial(2) returns 1*2=2
factorial(3) returns 2*3=6

hope this clears things up, if you understand the following method then you know recursion. For more you can take a look a wikipedia.

http://en.wikipedia.org/wiki/Recursion
 
Last edited:
recursion

shermaine80 said:
write recursive code for the following integer function:

power(x,n) is the exponential or power function x^n, where n is the exponent

For example: power(3,4) evaluates 3^4 which is 3 x 3 x 3 x 3 = 81

Expect the integer result to be large(ie in the millions)

explain how recursive code works.

Please help me how to write this code.
Thanks.

power(x, 1) is x, which is your "stopping" condition, and it needs to be checked first.

If n is an even number, power(x, n) is equal to power(x, n/2) squared. Otherwise power(x, n) is equal to x times power(x, n - 1).

A recursive function has two sections. The first section contains your "stopping" checks where the function just returns a result without calling itself. The second section contains decomposition code where a simplifying recursive expression is used, eventually reaching the stopping condition.
 
Hi Guys,

Thanks a lot! I got another question:

using recursive power function to calculate the equation

y(x,n) = x^n + x^n-1+x^n-2+x^n-3+...x^1 for n>=0

So the program will be something like

int x
long int factorial(int n)
{
if (n >=0)
return(1);
else
return(x * factorial (n-1));
}

may i know what does return (1) actually means?
Please advise. Thanks.
 
shermaine80 said:
Hi Guys,

Thanks a lot! I got another question:

using recursive power function to calculate the equation

y(x,n) = x^n + x^n-1+x^n-2+x^n-3+...x^1 for n>=0

So the program will be something like

int x
long int factorial(int n)
{
if (n >=0)
return(1);
else
return(x * factorial (n-1));
}

may i know what does return (1) actually means?
Please advise. Thanks.

To compute y(x, n) you just need to figure out the "stopping" condition, and the simplifying expression. The stopping condition is fairly easy, that y(x, 1) is just equal to x. Now for the simplifying expression you can figure that out by yourself.

int factorial(int n) {
if (n == 1) return(1);
else return(n * factorial(n -1));
}

Returning 1 just means that factorial(1) should be 1.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
11
Views
1K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
5
Views
12K
Back
Top