title: Most efficient way to calculate nCr modulo 142857
body: <p>I want to calculate <code>nCr modulo 142857</code>. Following is my code in Java:</p>
  
  <pre><code>private static int nCr2(int n, int r) {
      if (n == r || r == 0) {
          return 1;
      }
      double l = 1;
      if (n - r &lt; r) {
          r = n - r;
      }
      for (int i = 0; i &lt; r; i++) {
          l *= (n - i);
          l /= (i + 1);
      }
      return (int) (l % 142857);
  }
  </code></pre>
  
  <p>This gives nCr in <code>O(r)</code> time. I want an algorithm to get the result in less time than this. Is there such an algorithm?</p>