title: Modular multiplicative inverse function doesn't work for negative numbers
body: <p>I have the function below to calculate the  modular multiplicative inverse of a number n given the modulo number p.  </p>
  
  <pre><code>int modInverse(int n, int p) {
      n %= p;
      for(int x = 1; x &lt; p; x++) {
          if((n*x) % p == 1) return x;
      }
  }
  </code></pre>
  
  <p>If n is positive, it wokrs fine, but if n is negative it gives always 0. 
  How can I fix it?</p>
  
  <p>Multiplicative inverse of x mod n: x^-1 mod n, is the number that must be multiplied by x to get 1 mod n 
  e.g. 3^-1 mod 7 = 5, since 3 * 5 = 1 mod 7</p>