print -2 % 5 # prints 3 shitIt's because of two things: If one of the operands is negative, the result is flooredPython's integer division always return an integerThan -2/5 should be -0.4, but becomes -1Then applies the rule. Essentially, it's so that a/b = q with remainder r preserves the relationships b*q + r = a and 0 <= r < b.So 5 * -1 + r = -2 => -5 + r = -2 => r = 3!!!Credits:Kevin: http://stackoverflow.com/questions/3883004/negative-numbers-in-pythonGuido van Rossum: http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html I build a solution for the problem OneRegister from TopCoder in Java.It was giving the error:java.lang.OutOfMemoryError: Java heap spaceIt took me a while to figure out that the problem was happening because I was using int instead of long!!!Problem statement:http://community.topcoder.com/stat?c=problem_statement&pm=10992&rd=14239&rm=&cr=10574855package javaapplication1;import java.util.LinkedList;public class OneRegister { private final String NOT_POSSIBLE = ":-("; private String lexOrder(String s1, String s2) { char[] c1 = s1.toCharArray(); char[] c2 = s2.toCharArray(); for (int i = 0; i < s1.length(); i++) { if (c1[i] == c2[i]) { continue; } if (c1[i] == '*') { return s1; } return s2; } throw new IllegalArgumentException("the two strings are equal"); } class node { String p; int s; // <<<<<!!!!! replace with long node(String program, int s) { // <<<<<!!!!! replace with long p = program; this.s = s; } } public String getProgram(int s, int t) { if (s == t) { return ""; } if (t == 0) { return "-"; } if (t == 1) { return "/"; } if (s > t && t % 2 != 0) { return ":-("; } LinkedList<node> list = new LinkedList<>(); // + int sum = s + s; // <<<<<!!!!! replace with long if (sum <= t) list.add(new node("+", sum)); // * int mul = s * s; // <<<<<!!!!! replace with long if (mul <= t && mul != 1) list.add(new node("*", mul)); // / list.add(new node("/", 1)); String ans = NOT_POSSIBLE; while (!list.isEmpty()) { node n = list.poll(); if (n.s == t) { if (ans.equals(NOT_POSSIBLE) || lexOrder(ans, n.p).equals(n.p)) { ans = n.p; } continue; } if (!ans.equals(NOT_POSSIBLE) && n.p.length() >= ans.length()) { return ans; } // + sum = n.s + n.s; if (sum <= t) list.add(new node(n.p + "+", sum)); // * mul = n.s * n.s; if (mul <= t && mul != 1) list.add(new node(n.p + "*", mul)); } return ans; } public static void main(String[] args) { OneRegister app = new OneRegister(); System.out.println(app.getProgram(7, 392)); System.out.println(app.getProgram(7, 256)); System.out.println(app.getProgram(4, 256)); System.out.println(app.getProgram(7, 7)); System.out.println(app.getProgram(7, 9)); System.out.println(app.getProgram(10, 1)); System.out.println(app.getProgram(1000000000, 536870912)); System.out.println(app.getProgram(1000000000, 999999999)); System.out.println(app.getProgram(3, 402653184)); // <<< PROBLEM HERE! }}Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:2367) at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:130) at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:114) at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:415) at java.lang.StringBuilder.append(StringBuilder.java:132) at javaapplication1.OneRegister.getProgram(OneRegister.java:84) at javaapplication1.OneRegister.main(OneRegister.java:104)If you replace int with long where it's commented in the code, then it will work!!But why is that?public class IntLongTest { public static void main(String[] args) { System.out.println(Integer.MAX_VALUE); // 2147483647 System.out.println(Long.MAX_VALUE); // 9223372036854775807 // 2147483647 // 402653184 // 200000000 int x = 200000000; System.out.println(x * x); // -1090256896 what the fuck!! }}What happened was an Integer Overflow:http://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-foNote that if you use a recursive solution,