gudii9
asked on
bunnyEars challenge
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 the total number of ears across all the bunnies recursively (without loops or multiplication).
bunnyEars(0) → 0
bunnyEars(1) → 2
bunnyEars(2) → 4
please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
pseudeo code:
1. 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)
1. 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)
ASKER
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?
ASKER
i am reading from below link
http://introcs.cs.princeton.edu/java/23recursion/