private static int Remainder(int x, int modulus) { int rem = x % modulus; while (rem < 0) { rem += modulus; } return rem; }