Robert Plant - 1 year ago 42

Java Question

I'm supposed to write a "bisquare" method that returns the number of bisquares in a range of numbers. I thought I'd figured it out, but when I run this code, nothing displays and my laptop begins to whir like crazy. The system never says it finished.

Here is my code. What am I doing wrong? (I am also looking for a solution to the problem if I am not setting it up correctly.)

`// An integer that is the sum of the squares of two other integers is called bisquare`

// 5 is bisquare because it is 1*1 + 2*2

// 4 is bisquare because it is 0*0 + 2*2 (we can use 0 as one of our numbers)

// 8 is bisquare because it is 2*2 + 2*2 (we can use the same two numbers)

// 3 is not bisquare, 6 is not bisquare

//

// Given two int parameters, low and high, return the number of bisquares that

// fall between low and high (inclusive)

//

// EXAMPLES:

// low = 1, high = 6

// return 4

// 1, 2, 4, and 5 are bisquare. 3 and 6 are not

//

// low = 7, high = 7

// return 0

// 7 is not bisquare. that is the entire range we are checking.

public static int bisquare(int low, int high)

{

int count = 0;

boolean isBisquare = false;

for (int checkNum = low; checkNum < high; checkNum++) {

while (!isBisquare) {

for (int i = 0; i < high; i++) {

for (int j = 0; j < high; j++) {

if ((i*i) + (j*j) == low) {

count++;

isBisquare = true;

}

}

}

}

}

return count;

}

Answer

Yes,
If none of the `(i*i) + (j*j) == low`

evaluates to true the while will loop infinitely.