I'm trying to solve this riddle in java, about an old man who lives because his cult who gives the old man some of their life, this specific code should work to the rules which are given but one of the checks in the testing is an error.
public class hello {
/** set true to enable debug */
static boolean debug = true;
static long old(int n, int m, int k, int newp) {
int max;
int min;
boolean moreRows;
if (m > n) {
min = n;
max = m;
moreRows = true;
} else {
min = m;
moreRows = false;
max = n;
}
int sum = 0;
int[][] ar2 = new int[(int)m][(int)n];
// square part
for (int i = 0; i < min; i++) {
for (int j = 0; j < i; j++) {
int t = i ^ j;
ar2[i][j] = t - (t >= k ? k : 0);;
sum += 2 * t- (t >= k ? k : 0);;
}
}
for (int i = min; i < max; i++) {
for (int j = 0; j < min; j++) {
int t = i ^ j;
sum += t;
if (moreRows) {
ar2[i][j] = t - (t >= k ? k : 0);
} else {
ar2[j][i] = t;
}
}
}
//retrun time
while(newp<sum && newp>0) {
sum=sum-newp;//wrap it up
}
return sum;
}
}
here is the assert equals test which contains multiple examples:
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class HelloTest {
@Test
public void example() {
assertEquals(5, Hello.olde(8, 5, 1, 100));
assertEquals(224, Hello.old(8, 8, 0, 100007));
assertEquals(11925, Hello.old(25, 31, 0, 100007));
assertEquals(4323, Hello.old(5, 45, 3, 1000007));
assertEquals(1586,Hello.old(31, 39, 7, 2345));
assertEquals(808451, Hello.old(545, 435, 342, 1000007));
// You need to run this test very quickly before attempting the actual tests :)
assertEquals(5456283, Hello.old(28827050410L, 35165045587L, 7109602, 13719506));
}
}
i get the following errors which looks like a long to int-
./src/test/java/HelloTest.java:19: error: incompatible types: possible lossy conversion from long to int
assertEquals(5456283, hello.old(28827050410L, 35165045587L, 7109602, 13719506));
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
some more errors from harder examples-
assertEquals(5456283, Hello.old(28827050410L, 35165045587L, 7109602, 13719506));
^
./src/test/java/HelloTest.java:39: error: incompatible types: possible lossy conversion from long to int
long expected = Hello.old(m, n, l, t), actual = Hello.old(m, n, l, t);
debug some errors in one of the assert equals and harder examples, I can't really think what can be changed, I'm sure it's something small so help would be appreciated-Note: t will never be bigger than 2^32 - 1(from the instructions of the question) thanks