Created
July 6, 2013 22:09
-
-
Save mebubo/5941465 to your computer and use it in GitHub Desktop.
f(f(n)) == -n
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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