Solved

bunnyEars challenge

Posted on 2016-09-09
6
100 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 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

911 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now