public int bunnyEars(int n) {
if (n < 0) {
throw new IllegalArgumentException();
}
if (n == 0) {
return n;
}
if (n % 2 == 1)
return 2 + bunnyEars(n - 1);
return 3 + bunnyEars(n - 1);
}
can someone explain how bunnyEars(2) = 5
and also how it works?
If the number n is less than 0, then an IllegalArgumentException is thrown, as evidenced by:
if (n < 0) {
throw new IllegalArgumentException();
}
So, we know n is always supposed to be 0 or greater. We also know that the method is supposed to return when n is equal to 0, as evidenced by:
if (n == 0) {
return n;
}
So, presumably, the method public int bunnyEars(int n) will take a number equal to or greater than zero, and will start adding integers until the n is zero.
We see two different possible scenarios to take n's greater than zero and do something with them:
if (n % 2 ==1)
return 2 + bunnyEars(n-1);
return 3 + bunnyEars(n -1); //else
What is happening here is if the n % 2 is equal to one (meaning that the number is odd, since every odd integer divided by two has a remainder of one), then the method is recursively called with the current n minus 1, and the final integer to return is incremented by two.
If the number is not odd (and is thus even), then the same thing is happening, but with the final integer to return incremented by 3.
So in your example of bunnyEars(2), we see that the n of 2 is both positive and greater than zero, so no error is thrown and the method does not return without recursion. Since 2 is an even number (2 % 2 is 0), the second return is used. This means that 3 + bunnyEars(1) is called.
Since 1 is also greater than 0, we go to the recursive methods again. Since 1 is an odd number (1 % 2 is 1), the first recursive return is called. This means we call 2 + bunnyEars(0).
Now, since n is 0, the return n from the if-statement is used:
if (n == 0) {
return n;
}
On the first round we had the final integer to return incremented by 3, and on the second time it was incremented by 2, for a total of 5. So the result is five.
From your comments, I understand that you already know the meaning of a recursive call. In order to make it clear how it works, you can trace the calls in some way. Given below is an example of one of the ways:
public class Main {
public static void main(String[] args) throws InterruptedException {
System.out.println(bunnyEars(2));
}
static int bunnyEars(int n) {
if (n < 0) {
throw new IllegalArgumentException();
}
if (n == 0) {
System.out.println(n);
return n;
}
if (n % 2 == 1) {
System.out.print("2 + bunnyEars(" + n + "-1) -> ");
return 2 + bunnyEars(n - 1);
}
System.out.print("3 + bunnyEars(" + n + "-1) -> ");
return 3 + bunnyEars(n - 1);
}
}
Output:
3 + bunnyEars(2-1) -> 2 + bunnyEars(1-1) -> 0
5
As you can see, 3 + 2 + 0 = 5 is what you get as the answer.
I hope, it helps. Feel free to comment in case of any doubt.
Related
The code below has no output when I run it, I think somehow it is infinite loop? How to fix it?
Write a method named getEvenDigitSum with one parameter of type int called number.
The method should return the sum of the even digits within the number.
If the number is negative, the method should return -1 to indicate an invalid value.
EXAMPLE INPUT/OUTPUT:
getEvenDigitSum(123456789); → should return 20 since 2 + 4 + 6 + 8 = 20
getEvenDigitSum(252); → should return 4 since 2 + 2 = 4
getEvenDigitSum(-22); → should return -1 since the number is negative
public class getEvenDigitSum {
public static int getEvenDigitSum(int number) {
int sum = 0;
int lastDigit=0;
if (number < 0) {
return -1;
}
while (number >0) {
lastDigit = number % 10;
if (number % 2 == 0)
{
sum += lastDigit;
number = number / 10;
}
}
return sum;
}
}
while (number >0) {
lastDigit = number % 10;
if (number % 2 == 0)
{
sum += lastDigit;
number = number / 10;
}
}
OK, and what happens if the number isn't even? You didn't make an else/else if statement after; if your number is odd, it stays the same, the loop is infinite, and hence your code does nothing.
Your condition only handles even digits, but think about what happens when you get a number that contains odd digit, like 1221 for example.
Try adding the missing else statement:
while (number >0) {
lastDigit = number % 10;
if (number % 2 == 0)
{
sum += lastDigit;
number = number / 10;
}
else {
// Deal with odd digit, leaving for you to implement
{
}
General tip: The best way to find errors in your code is to write tests and debug your code.
These are two skills every developer should poses and practice regularly
I'm trying to solve a recursive function practice problem using Java and it has me completely stumped:
Given the small integer n (0 <= n <= 40) you need to find the n-th number of the alternating Fibonacci sequence.
The sequence starts with 0, 1, -1, 2, -3, 5, -8, 13, -21, ...
So, fib(0) = 0, fib(1) = 1 => fib(2) = -1.
I can implement the function to find the Nth fibonacci number, however the specific problem requirements are defeating me. Any time I try to implement some sort of negative number, it ends up screwing up the arithmatic instead of altering the final number that is output. My mind keeps coming back to creating some sort of conditional that only triggers on the top-most frame, but I don't think that is something that can be implemented.
Does anyone have an idea how to solve this? This is my base function without implementing any sort of the negative number requirements:
public static long fib(long n){
if (n == 0){
return 0;
} else if (n == 1){
return 1;
} else if (n == 2){
return 1;
} else {
return fib(n-2)+fib(n-1);
}
}
You can simply have another function to deal with the negative requirement:
public static int AlternatingFiboonacci(int n){
if(n > 0 && n % 2 == 0) return -fib(n); //if n is even and greater than 0
else return fib(n);
}
If you need a single working function, this should do the job
public static int fib(int n){
if(n < 2) return n;
if(n % 2 == 0) return -1 * (fib(n - 1) - fib(n - 2));
else return (-1 * fib(n - 1)) + fib(n - 2);
}
What this function does is:
when n is even, return fib(n - 1) (which is odd, thus positive) - fib(n - 2) (which is even, thus negative). The subtraction will be a positive value, that you multiply by -1.
when n is odd, return -1 * fib(n - 1) (which is even, thus negative) + fib(n - 2) (which is odd, thus positive).
Maybe it's not too late to put this as an answer:
public static long fib(long n){
if (n <= 1){
return n;
} else {
return fib(n-2) - fib(n-1);
}
}
You can first get your number:
public static long fib(long n) {
if ((n == 0) || (n == 1))
return n;
else
return fib(n - 1) + fib(n - 2);
}
And then add the minus sign if necessary:
public long result(long n){
long fib = fib(n);
if(n>0 && n%2==0) return -fib;
else return fib;
}
Just think of the formula for this one.
You want normal Fibonacci sequence but on even position they are negative. Let's say your method will be named altFib. If you apply Math.abs( altFib(n) ), you get actual value for n-th Fibonnaci number, so it's obvious that below code will also result with n-th Fibonacci number:
int fib_n = Math.abs( altFib(n-1) ) + Math.abs( altFib( n-2 ) )
Then you want it to be negative on even positions, so just use simple if else:
if( n % 2 == 0 )
return -fib_n
else
return fib_n
Write a Processing method to return one of three strings, depending on the value of the parameter x. If x is even, the method should return “Even”. If x is divisible by three, the method should return “By three”. If x is neither even nor divisible by three, the method should return “Just odd”.
The signature for your method should be String evenOdd(int x)
The issue with your code, which you have left in the comments, is that they are asking for the number to be divisible by three, meaning a remainder of 0. Your code is trying to find a remainder of 3.
so, rather than writing if (x % 3 == 3), say if (x % 3 == 0).
Basically your full code would look as follows:
string evenOdd(int x)
{
string theResponse = "";
if (x % 2 == 0) // if x is divisible by 2 with no remainders
{
theResponse = "Even!";
}
else if (x % 3 == 0) // if x is divisible by 3 with no remainders
{
theResponse = "By three!";
}
else
{
theResponse = "Odd";
}
return theResponse;
}
String evenOdd(int x)
{
if (x%2 == 0) //number is even when it is divisible by 2
return "Even";
else if(x%3 == 0) //if remainder is 0 then it is divisible by three
return "By three";
else
return "Just Odd";
}
recently I have been working on an assignment to find the fibonacci sum of a number (user input, positive integer) in recursive form, for example (output is):
9 = 8 + 1
14 = 13 + 1
30 = 21 + 8 + 1
and so on.
So far, I have made the recursive function to calculate the actual fibonacci numbers (such as 8 and 1 in the sum of 9), it looks like this :
static long[] f = new long[50];
static long fib(int n) {
if (n <= 1) { //base case
return 1;
}
if (n < f.length) {
if (f[n] != 0) {
return f[n];
}
return f[n] = fib(n - 1) + fib(n - 2);
}
return fib(n - 1) + fib(n - 2);
}
My assignment gives me this as a hint:
Hints:
Re-cast the theorem as
n = fj + (n - fj )
This suggests a recursive solution.
and with that, I have currently come up with this:
static void fibSum(int n)
{
System.out.print(n + " = ");
for(int i = 0; i >= 0 ; i++)
{
if(n - (fib(i)) == 0)
{
System.out.println(fib(i));
}
else if(n < (fib(i)) && n > (fib(i-1)))
{
System.out.println(fib(i-1) + " + ");
}
}
}
My output goes as far as [entering for example, 9 as the user input] '9 = 8 +', and with that said, my question for all of those who are reading this (and thank you for getting this far!) is why I am not getting the last number (1) in the broken-down sum, and is my solution considered recurisve, as it really does not follow the format that I have seen in past examples, and in the fib() method that I wrote. I did not know how to implement printing the plus signs through that format.
Here is my main method for reference:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter an integer number: ");
int n = scan.nextInt();
fibSum(n);
You seems to have various issues in your function fibSum:
Your For loop does not have an end. (while i is positive, increment i.)
You can store: fib(i) in your for loop to avoid multiple calculations.
You don't handle every cases on your if/else condition.
Exemple:
n = 2.
fib(0) = 1
if(2 - 1 == 0) => false
if(2 < 1 && 2 > fib(-1)) => false && error.
// Displays nothing. Should display: 2 = 1 + 1
Finally figured it out, this time using a while loop. No matter how many times I tried fixing the for loop, I ran into issues with my output, so I used a boolean variable and simply made it false once I reached n = 0.
I am revising for a software testing exam. One of the questions gives this method and asks to identify the fault as well as produce a test case (if one exists) which does not execute the fault.
Here is the code:
public static int oddOrPos(int[] x) {
//Effects: if x==null throw NullPointerException
// else return the number of elements in x that
// are either odd or positive (or both)
int count = 0;
for (int i = 1; i < x.length; i++)
{
if (x[i]%2 == 0 || x[i] > 0)
{
count++;
}
}
return count;
}
I have identified two problems. One being that i is initialised to 1 in the for loop so x[0] doesn't get tested. Also x[i] % 2 == 0 should be x[i] != 0
Are these problems faults or errors? I ask this because the question makes it appear that there is only one fault.
Also, I assume that because the for loop will always be executed, there is no test case which will not execute the fault.
Actually x[i] % 2 == 0 should be x[i] % 2 != 0 (if we want to detect odd values along with the positive ones. The existing code will detect even values instead).
The test case is just { -2 } - this element is even and negative, so should not get counted, and the method will return 0 even though it is faulty. { 1 } will also give 0, which is wrong.
If you want to detect odd negative values you'll have to look for -1 and not for 0 as it's done right now.
For odd positive values it will be 1. So basically you want anything but 0.
The % operator is a remainder operator, not really a modulo operator, it returns a negative number if the first given number is negative:
class Test1 {
public static void main(String[] args) {
int a = 5 % 3; // 2
int b = 5 / 3; // 1
System.out.println("5%3 produces " + a +
" (note that 5/3 produces " + b + ")");
int c = 5 % (-3); // 2
int d = 5 / (-3); // -1
System.out.println("5%(-3) produces " + c +
" (note that 5/(-3) produces " + d + ")");
int e = (-5) % 3; // -2
int f = (-5) / 3; // -1
System.out.println("(-5)%3 produces " + e +
" (note that (-5)/3 produces " + f + ")");
int g = (-5) % (-3); // -2
int h = (-5) / (-3); // 1
System.out.println("(-5)%(-3) produces " + g +
" (note that (-5)/(-3) produces " + h + ")");
}
}
Another "small" fault is the way the condition is done. Instead of checking for odd or positive, looking for positive or odd will be slightly faster. It's only because it's easier to check if a number is positive or not than getting its remainder.
Resources:
15.17.3. Remainder Operator %
The major thing here is that your for loop is starting at 1, and it should start at 0. You will always miss the first element of the array. Also x[i]%2 == 0 returns true for even numbers, not odd. So change that to x[i]%2 != 0.
public class test{
public static void main(String[] args){
int[] x = {3, 5, -1, -14}
if( 3 == oddOrPos(x)){
System.out.println("Working");
else
System.out.println("Test Fail");
}
public static int oddOrPos(int[] x) {
//Effects: if x==null throw NullPointerException
// else return the number of elements in x that
// are either odd or positive (or both)
int count = 0;
for (int i = 0; i < x.length; i++)
{
if (x[i]%2 != 0 || x[i] > 0)
{
count++;
}
}
return count;
}
}
As I understand it, you are right in your assumption. The first position of the array should be tested, hence the i[0] you pointed out.
However, x[i]%2 == 0 should instead be x[i]%2 == 1 for an odd number.