Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

bunnyEars challenge

Posted on 2016-09-09
6
Medium Priority
?
282 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
6 Comments
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 500 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 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 32

Accepted Solution

by:
awking00 earned 1000 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

In this post we will learn different types of Android Layout and some basics of an Android App.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Screencast - Getting to Know the Pipeline
Suggested Courses

971 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