Skip to content

Instantly share code, notes, and snippets.

@mebubo
Created July 6, 2013 22:09
Show Gist options
  • Select an option

  • Save mebubo/5941465 to your computer and use it in GitHub Desktop.

Select an option

Save mebubo/5941465 to your computer and use it in GitHub Desktop.
f(f(n)) == -n
import org.junit.Assert;
public class FFN {
public static int f(int n) {
if (n > 0) {
if (n % 2 == 0) {
return n - 1;
} else {
return -n - 1;
}
} else if (n < 0) {
if (n % 2 == 0) {
return n + 1;
} else {
return -n + 1;
}
} else {
return 0;
}
}
public static void check(int i) {
Assert.assertEquals(-i, f(f(i)));
}
@org.junit.Test
public void testF() throws Exception {
for (int i = Integer.MIN_VALUE + 2; i < Integer.MAX_VALUE; i++) {
if (i % 100_000_000 == 0) {
System.out.println(i);
}
check(i);
}
check(Integer.MAX_VALUE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment