Solved

bunnyEars challenge

Posted on 2016-09-09
6
64 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 31

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

758 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

24 Experts available now in Live!

Get 1:1 Help Now