Solved

bunnyEars challenge

Posted on 2016-09-09
6
119 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

808 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