Why doesn't this loop terminate? - java

Here's the sample code:
public static void col (int n)
{
if (n % 2 == 0)
n = n/2 ;
if (n % 2 != 0)
n = ((n*3)+1) ;
System.out.println (n) ;
if (n != 1)
col (n) ;
}
this works just fine until it gets down to 2. then it outputs 2 4 2 4 2 4 2 4 2 4 infinitely. it seems to me that if 2 is entered as n then (n % 2 == 0) is true 2 will be divided by 2 to yeild 1. then 1 will be printed and since (n != 1) is false the loop will terminate.
Why doesn't this happen?

Because when you get to 1, you are multiplying by 3 and adding 1, taking you back to 4.
You need an ELSE in there. I don't know java, but it would look something like:
public static void col (int n)
{
if (n % 2 == 0)
n = n/2 ;
else if (n % 2 != 0)
n = ((n*3)+1) ;
System.out.println (n) ;
if (n != 1)
col (n) ;
}
EDIT: as mentioned in the comments, you can omit the if test after the else:
if (n % 2 == 0)
n = n/2 ;
else
n = ((n*3)+1) ;

I think you'll have to change the 2nd if statement to an else
if (n % 2 == 0) // if the n is even
n = n/2 ;
else // if n is odd
n = ((n*3)+1) ;

The answer to the question can be read directly in the code:
Assume n is 2
(n % 2 == 0) is true therefore n <- 1
(n % 2 != 0) is also true therefore 4 <- n
this warrants a call to function with n = 4, which is then changed to 2 and
"back to square 1"
by replacing the second test by an else, you solve this logic problem, at the cost of possibly causing more recursion (since in the current logic, two operations are sometimes performed in one iteration). Such a fix will also solve a more subtle bug, which is that in the current version not all new values of n are printed out.
Now, for extra credit, prove that not matter the initial value of n, the number of recursions is finite (i.e. the sequence converges to 1). ;-)

Use if/then/else. Your logic is wrong.

when the input is 2:
if (n % 2 == 0) //true
n = n/2; //n = 1
if (n % 2 != 0) //true
n = ((n*3)+1); //n = 4
System.out.println (n); //prints 4
if (n != 1) //true
col (n); //call col(4)

Does it work if you change it to this?
if (n % 2 == 0)
n = n/2 ;
else if (n % 2 != 0)
n = ((n*3)+1) ;
It looks like you're getting 2, dividing by 2 to get 1, then checking to see if 1/2 has a remainder (it does), and multiplying it by 3 and adding 1, to get 4....

if (n % 2 != 0)
n = ((n*3)+1) ;
this code is again implemented whenever u get 1.
therefore the recursive function will be called repeatedly hence leading to an infinite rec calling and code will never terminate.

in addition to an else if to govern the condition that n is odd that same line also needs & n != 1 to be added to it within the conditional. So this:
else if (n % 2 != 0 & n != 1)

Related

The number of option to fill a given shape

Given a two 1d arrays that are stick one to the other with n and m lengths , write a recursive algorithm to find the number of ways that this shape could be filled by 1x1 or 1x2 or 2x1 blocks ,
here is my attempt , but I believe that I'm counting the same option several times :
public static int foo(int n1 ,int m){
if(n1==0 && m ==0){
return 1;
}
if(n1 < 0 || m < 0)
return 0;
return (foo(n1-1,m)+foo(n1,m-1)+foo(n1-1,m-1) +foo(n1,m-2) + foo(n1-2,m));
}
*** UPDATE ****
now the code compiles.
Examples :
input foo(2,2) output : 21 , the right answer is 7 .
input foo(4,3) output : 417, the right answer is 32.
these are the options for foo(2,2).
We'll assume n < m. If this is not the case we can just reverse the arguments - this makes the code simpler.
Once we've dealt with the terminating conditions we use a decrease-and-conquer strategy to reduce the input according to the following rules: if n == m, we can reduce both n & m by 1 two ways, n & m by 2 one way, n by 1 and m by 2 one way, and n by 2 and m by 1 one way. If n < m we can reduce m by 1 one way and m by 2 one way.
static int foo(int n, int m)
{
if(n > m) return foo(m, n);
if(n < 0 || m < 0) return 0;
if(n == 0 && m == 0) return 1;
if(n == m) return 2*foo(n-1, m-1) + foo(n-2, m-2) + foo(n-1, m-2) + foo(n-2, m-1);
return foo(n, m-1) + foo(n, m-2);
}
Test:
for(int i=0; i<5; i++)
for(int j=i; j<5; j++)
System.out.format("(%d, %d) = %d%n", i, j, foo(i, j));
Output:
(0, 0) = 1
(0, 1) = 1
(0, 2) = 2
(0, 3) = 3
(0, 4) = 5
(1, 1) = 2
(1, 2) = 3
(1, 3) = 5
(1, 4) = 8
(2, 2) = 7
(2, 3) = 10
(2, 4) = 17
(3, 3) = 22
(3, 4) = 32
(4, 4) = 71
For the case n == m (2, 7, 22, 71, ...) this is a known integer sequence (A030186).
And just for reference, here are the 32 configurations for (3,4):
I believe that i have found the correct answer to my question :
yet i'm not closing this problem until someone with better knowledge than me confirm my answer
public static int foo(int n1 ,int m){
if(n1==0 && m ==0){
return 1;
}
if(n1 < 0 || m < 0)
return 0;
if(m == n1){
return Integer.max(foo(n1-1,m),foo(n1,m-1)) + Integer.max(foo(n1-2,m),foo(n1,m-2))+ foo(n1-1,m-1);
}else{
return Integer.max(foo(n1-1,m),foo(n1,m-1)) + Integer.max(foo(n1-2,m),foo(n1,m-2));
}
}
now i'm taking only the maximum sub-Problem answer so I won't count the same option more than once.

FizzBuzz: It gives me the wrong print statement. What is wrong?

Everybody knows that FizzBuzz question that interviewers ask students.
Basically, when you have an incrementor and for each number which is a divisible of 3 you say fizz, for a number divisible by 5 you say buzz, while if it is divisible by both(3 and 5) you say FizzBuzz, hence the name.
It is a relatively easy problem to solve and I have done it, but I think my solution is a bit clunky. This is it:
int[] numbers = new int[100];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i;
}
for (int i : numbers) {
if (i % 3 == 0) {
System.out.println("Fizz");
} else if(i % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println("FizzBuzz");
}
}
But the problem is that when the number is divisible by both 3 and 5 it gives me "Fizz" for some reason. Can somebody explain to me, because I'm new to java programming. Thanks in advance!
The problem lies in the order of your if statements. Lets take a look at the number 15, which is the first number divisible by both 3 and 5. Because of the order in which you have your if statements, the first statement that is checked is
if ( 15 % 3 == 0)
The result of the operation is indeed equal to 0, as 15 is divisible by 3 and so "Fizz" is printed and your else is ignored.
Think about how you should structure the order of your if statements and which additional condition should you introduce to catch the specific case of being divisible by both i % 3 == 0 && i % 5 == 0.
When you enter the if statement and your number is 15 for exemple, you enter the first if statement and.. prints "Fizz" as you stated, because 15 % 3 == 0 returns true. Then it ignores the else.
You want the first if to be
if (i % 3 == 0 && i % 5 == 0) {
System.out.println("FizzBuzz");*
}
Try this code
public static void main(String[] args) {
int[] numbers = new int[100];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i;
}
for (int i : numbers) {
if ((i % 3 == 0) && (i % 5 == 0)) {
System.out.println("FizzBuzz");
} else if(i % 5 == 0) {
System.out.println("Buzz");
} else if (i % 3 == 0){
System.out.println("Fizz");
}
}
}

multiple conditions else / if statement somehow wrong?

I'm doing a hackernet challenge where n is an int input. The conditions are:
If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5, print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird.
Im sure the code makes logic and dont think theres syntax. It gives the correct responses and hackernet still says its incorrect so ive come here to see if anyone can see what the problem is
public static void main(String[] args)
{
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if (N % 2 != 0 || N % 2 == 0 && N >= 6 && N <= 20)
{
System.out.print("weird");
}
else
{
System.out.print("not weird");
}
}
The problem is the logic in your else condition, which would also catch values of N which are less than 2. Try this version:
if (N % 2 != 0)
{
System.out.print("weird");
}
else if (N >= 2 && N <= 5 || N > 20)
{
System.out.print("not weird");
}
else if (N >= 6 && N <= 20)
{
System.out.print("weird");
}
else
{
// NOTE: if the code still fails, remove this else condition
System.out.print("unexpected value of N");
}
Note: To get your code to pass the Hackernet task, you might have to completely remove the else condition. I added it for completeness, but Hackernet might test N=1 to see if nothing gets printed.
Read this condition :
if (N % 2 != 0 || N % 2 == 0 && N >= 6 && N <= 20)
as
if (N % 2 != 0 || (N % 2 == 0 && N >= 6 && N <= 20))
Then see how operator precedence changes the behaviour and yield desired results.
Check the following one
public static void main(String[] args)
{
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if(N%2!=0) {
System.out.print("weird");
}else if(N>=2 && N<=5) {
System.out.print("not weird");
}else if(N>=6 && N<=20) {
System.out.print("weird");
}else if(N>20) {
System.out.print("not weird");
}
}
For the technical part: start by reading about
precedence of java operators and then make your code easier to read.
Pushing that many conditions into a single if is not helpful. You see it yourself: you think the code is correct, but probably it isn't. And now you look to other people to explain your overly complex code back to you. And of course, all the other answers do all that for you ... but beyond that:
The "real" answer here is: learn how to test your code.
Instead of having a main that somehow asks for a number, and then makes decisions, write a method boolean isWeird() that takes a number and returns true/false according to your requirements.
And then simply test that method with all reasonable cases. And then check if that result is as expected.
Using JUnit, you could write something like
assertThat(isWeird(1), true);
assertThat(isWeird(21), true);
assertThat(isWeird(22), true);
...
Ideally, you write such tests before you implement that method. And then you implement all the conditions, and any check that fails tells you that you got something wrong.
I feel, In the if (N % 2 != 0 || N % 2 == 0 && N >= 6 && N <= 20) condition, you are verifiying the odd and even values at same time using && and || operator. Can you modify the condition into like this if (N % 2 != 0 || (N % 2 == 0 && N >= 6 && N <= 20)) and check? If N is odd weird will be printed or if N is even and it falls under the 6 and 20 inclusive, weird will be printed.
You already have a good few answers here but if you think logically about what you actually need, you can break it down easier.
It looks like the only "Not Weird" print out is 2, 4 and even numbers > 20
So an example could be something like:
if (n % 2 == 0) {
if ((n >= 2 && n <= 5) || (n > 20)) {
return "Not Weird";
}
}
return "Weird";
You can try this
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if (n % 2 == 1 || (n >= 6 && n <= 20)) {
System.out.println("Weird");
} else {
System.out.println("Not Weird");
}
scanner.close();
}

Simple programming practice (using modulus op)

First question, I'm trying to do a practice where I am asked to return true if the given non-negative number is a multiple of 3 or 5, but not both. Examples are;
old35(3) → true
old35(10) → true
old35(15) → false
So 3 would be true, as it's a multiple of 3, but I can't currently make my code work for all instances. Code below;
public boolean old35(int n) {
if ((35 % n == 3) || (35 % n == 5))
return true;
return false;
}
How could I edit this to fit the criteria of what the practice is telling me to do?
>
I will throw a detailed explanation. First, you use the remainder sign to check the condition where remainder is zero;
n % 3 == 0
and
n % 5 == 0
Then for a one to one translation for your conditions, put them together as:
((n % 3 == 0) || (n % 5 == 0)) // Multiple of 3 or 5
!((n % 3 == 0) && (n % 5 == 0)) // But not both
Add (and) them together;
public static boolean old35(int n) {
return(((n % 3 == 0) || (n % 5 == 0)) && !((n % 3 == 0) && (n % 5 == 0)));
}
But simply, what you are doing is the XOR (^) operation, the following code is valid and simple:
public static boolean old35(int n) {
return((n % 3 == 0) ^ (n % 5 == 0));
}
You can use the logical XOR of the conditions. Basically you can do C1 ^ C2.
Edit: To check if multiple of 3 you must check n % 3 == 0, the same for multiple of 5.

What is meaning of expression while(n&3)==0 and n>>=2

I find confused with these expression while(n&3)==0 and n>>=2 . I am not sure when this condition is executed while((n&3)==0) and what happens n>>=2
public int numSquares(int n) {
while ((n & 3) == 0) //n % 4 == 0
n >>= 2;
if ((n & 7) == 7) return 4; //n% 8 == 7
if(is_square(n)) return 1;
int sqrt_n = (int) Math.sqrt(n);
for (int i = 1; i<= sqrt_n; i++){
if (is_square(n-i*i)) return 2;
}
return 3;
}
public boolean is_square(int n){
int temp = (int) Math.sqrt(n);
return temp * temp == n;
}
& is a binary AND operator. 3's representation in binary is 0000..0011. Therefore, the condition
(n & 3) == 0
is true when the last two bits of n are both set to zero. This happens when the number is divisible by 4, as suggested by the n % 4 == 0 comment.
Similarly, (n & 7) == 7 means "the last three bits of n are all set to 1", because the binary representation of 7 is 000..00111. Again, this is equivalent to having the remainder of 7 after dividing by 8, hence the n% 8 == 7 comment.
When you do n>>=2, you shift the number by two bits to the right, with sign extension. In your context it is equivalent to division by four, because the loop stops as soon as n is no longer divisible by four.
(n & 3) == 0 is an overly complex way of saying "n is a multiple of 4".
n>>=2 is an overly complex way of saying "divide n by 4, rounding down to the next lowest integer".
So this loop means "Keep dividing n by 4 until it's no longer a multiple of 4".
& is a bitwise AND
Bitwise operator works on bits in case of AND it return 1 only if both operands are 1, otherwise zero.
Suppose n = 4 in your case then
n & 3 would be 100 & 011 will give you 000 ie 0
>> bitshift operator
n >> 2 would be 100 >> 2 give you 001 ie 1
shifting each bit to the right 2 times.
You can read about the more in Docs

Categories