Solved

bunnyEars challenge

Posted on 2016-09-09
6
222 Views
Last Modified: 2016-09-13
Hi,

I am working on below challenge
http://codingbat.com/prob/p183649


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
I am not clear on above desription. what it mean by computing recursively?
 please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 125 total points
ID: 41791988
A "recursive method" is one that calls itself in order to compute a value. Explaining much more about the concept would pretty much solve the challenge for you. You have a better start going in one of the other questions you just posted.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41792180
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/
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 125 total points
ID: 41793821
So in fact you're asking what recursion is. I would use Google for that.
Eg.
Explanation of recursion
An introduction to recursion
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 32

Accepted Solution

by:
awking00 earned 250 total points
ID: 41794832
Maybe a general description of what this challenge is doing will offer a better understanding of recursion. We know that if there are no bunnies, there will be no ears (the base case). We can also agree that if there is one bunny, it will have two ears. But if you look a little deeper for the case of 1 bunny, you can see that it is equivalent to 2 plus the bunnyEars(0) function call which is equivalent to 2 + bunnyEars(bunnies - 1). If bunnies equal 2, that's equivalent to 2 + bunnyEars(1). If we keep making calls to the bunnyEars function passing in the bunnies value - 1 and adding 2, we eventually will reach the point where bunnies = 0 (i.e. the base case) and the function will "recursively" return the number of ears dictated by the value of bunnies. Hope this helps a little bit.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41796790
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)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41796795
public int bunnyEars(int bunnies) {
 if(bunnies==0){
   return 0;
 }
 else{
   return 2+bunnyEars(bunnies-1);
 }
}

Open in new window


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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Simple Linear Regression

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question