$n! \bmod c$ where $c$ is a composite number

126 Views Asked by At

I am trying to write a program to calculate what is $n! \bmod c$, where $c$ is a composite number. While I understand $a b \bmod c$ is equal to $((a \bmod c) (b \bmod c)) \bmod c$, when I use this formula my program returns $0$ for any result greater than $c$. How do I do it correctly?

1

There are 1 best solutions below

0
On BEST ANSWER

If I understood the question, you program simply works good!

If you think about it, $n!$ is a pretty huge number with plenty of divisors, and if $n > c$ you get $n! = 1 \cdot 2 \cdot 3 \dots \cdot (c-1) \cdot c \cdot (c+1) \cdot \dots \cdot (n-1) \cdot n \equiv 1 \cdot 2 \cdot \dots (-1) \cdot 0 \cdot 1 \dots \equiv 0 \mod c $

Therefore $n! \equiv 0 \mod c$ of $n > c$