3

It is well known that we easily can compute , say , $2^a\mod b$ for large integers $a,b$. We can use the repeated square method which gives a fast result even if $a,b$ have , say , $50$ decimal digits.

Is there a similar trick to compute $a!\mod b$ for , say , $50$ decimal-digit-numbers $a$ and $b$ ? We can assume that we know the factorization of $b$ , in fact , I am particular interested in the case that $b$ is prime. I would like to search prime factors of , say , $(18!)!-1$.

It is hopeless to apply $a-1$ multiplications modulo $b$. We can use Wilson's theorem , if $b-a$ is small , but I have little hope in the general case.

Bill Dubuque
  • 282,220
Peter
  • 86,576
  • 3
    Try a! \bmod b for a better spacing (the “binary mod” operator). – Martin R Jun 14 '24 at 21:40
  • 4
    For sufficiently large $a$, $a! \bmod b = 0$. – Dan Jun 14 '24 at 22:34
  • 2
    I am pretty sure there is no good algorithm for general $a<b$ when $b$ is prime. Competitive programmers have to calculate this number in a lot of contest problems, and I have only seen it being calculated in $O(a)$ multiplications. There was one niche algorithm which calculated it in $O(\sqrt{a}\log^3 a)$ multiplications, but the constant in it is so large that it only starts getting useful when $a > 5\cdot10^8$, and the running time is already over $5$ seconds for a small $b$ by then. – EnEm Jun 14 '24 at 23:10
  • I can describe the $O(\sqrt{a}\log^3 a)$ algorithm if you want. – EnEm Jun 14 '24 at 23:11
  • $\def\ed{\stackrel{\text{def}}{=}}$ If $\ p,q\ $ are prime, with $\ q>p\ ,$$,b\ed pq\ ,$ and $\ f\stackrel{\text{def}}{=}\left\lfloor\frac{b}{2}\right\rfloor!\bmod pq\ ,$ then $\ p=\gcd(f,b)\ .$ Thus, if there were a procedure for calculating $\ a!\bmod b\ ,$ for arbitrary composite $\ b\ $ that was as efficient as that for exponentiation, then it could be used to factorise the product of two primes thousands of bits long efficiently, and hence break the Cocks-RSA cryptosystem. – lonza leggiera Aug 24 '24 at 16:34
  • The discovery of any such procedure would therefore be hugely significant, and extremely difficult. – lonza leggiera Aug 24 '24 at 16:34
  • We could use things like even mod odd is opposite parity to the multiplier. b needs to greater than a

    Since all factorials greater than 3 are divisible by 6, we have ((0 mod 6) mod (1 mod 6)) (1,5) (2,4)(3,3) (multiplier, remainder mod 6 left) etc.

    – Roddy MacPhee Apr 01 '25 at 21:38

0 Answers0