I\'m writing a program (using java) that is suppose to find the lowest integer t
ID: 3549742 • Letter: I
Question
I'm writing a program (using java) that is suppose to find the lowest integer that can be evenly divided by a range (ex. 1-25). I need help fixing/debugging it so it comes up with the proper output. this is the part of my code that does the calculations. I looked up ways to do it online but they are using different methods and I don't want figure it out what I'm doing wrong with my own code. Please help! And thank you!
void main(String[] args){
int num = lowest(1,25);
System.out.println("The smallest divisible number is" + num);
}
public static int lowest(int lower_limit, int upper_limit){
boolean find = false;
int L = upper_limit;
while (find) {
boolean yes = true;
for (int j=lower_limit; j<= upper_limit; j++) {
if (j/L != (L/j ))
break;
yes = false;
}
if (yes){
find = true;
}
else{
L++;
}
}
return L;
}
}
Explanation / Answer
here iam going to write the code for the function.
public static int lowest(int lower_limit, int upper_limit) {
int L = upper_limit;
for (int j = upper_limit; j>=lower_limit;j--)
{
if (L %j != 0)
L = L * j;
}
return L;
}
% is MOD operation. L %j gives the remainder when L is divided by j.
the logic is: if the number( L) is not divisible by a number (j) , L*j will be divisible by j.
let me explain through an example.
Q: smallest integer that is divisible by range(1-9)
L = 9
(9 % 9 ! = 0) is false so L is unchanged
(9 % 8 ! =0 ) is true so L = 9 *8 = 72
(72 % 7 !=0) is true so L= 72 * 7 = 504
(504 % 6 !=0) is false so L is unchanged
(504 % 5 !=0) is true so L = 504 * 5 = 2520
(2520 % 4 != 0) is false so L is unchanged
(2520 % 3 !=0) is false so L is unchanged
(2520 % 2 !=0) is false so L is unchanged
(2520 % 1 !=0) is false so L is unchanged
the loop terminates once it reaches lower_limit.
now L= 2520 which is the lowest integer divisible range (1-9).