The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Hi,

I am working on below challenge

http://codingbat.com/prob/p183649

please advise

I am working on below challenge

http://codingbat.com/prob/p183649

I am not clear on above desription. what it mean by computing recursively?

We have a number of bunnies and each bunny has two big floppy ears. We want to compute thetotal number of ears across all the bunnies recursively (without loops or multiplication).

bunnyEars(0) → 0

bunnyEars(1) → 2

bunnyEars(2) → 4

please advise

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The idea of calling one function from another immediately suggests the possibility of a function calling itself. The function-call mechanism in Java supports this possibility, which is known as recursion.

Your first recursive program. The "Hello, World" for recursion is the factorial function, which is defined for positive integers n by the equation

n!=n×(n−1)×(n−2)×…×2×1

n!=n×(n−1)×(n−2)×…×2×1

The quantity n! is easy to compute with a for loop, but an even easier method in Factorial.java is to use the following recursive function:

public static long factorial(int n) {

if (n == 1) return 1;

return n * factorial(n-1);

}

We can trace this computation in precisely the same way that we trace any sequence of function calls.

factorial(5)

factorial(4)

factorial(3)

factorial(2)

factorial(1)

return 1

return 2*1 = 2

return 3*2 = 6

return 4*6 = 24

return 5*24 = 120

i am reading from below link

http://introcs.cs.princeton.edu/java/23recursion/

Eg.

Explanation of recursion

An introduction to recursion

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial1. if o bunnies return ears 0;

2. if one bunny ears 0 i.e 2+bunnyEars(1-1)

3. if n bunnies 2+bunneyEars(n-1)

```
public int bunnyEars(int bunnies) {
if(bunnies==0){
return 0;
}
else{
return 2+bunnyEars(bunnies-1);
}
}
```

i passed all the tests. any improvements or alternate approach?

Java

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.