subfactorial modulo prime n mod p
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction to Subfactorial Modulo Prime
The concept of a subfactorial, denoted as !n
, is an important topic in combinatorics. It represents the number of derangements (permutations such that no element appears in its original position) of a set of n
objects. When combined with number theory, specifically modular arithmetic with primes, the study of subfactorial modulo prime, !n mod p
, introduces a fascinating area with diverse applications in cryptography, algorithm design, and theoretical computer science.
Derangement: Understanding Subfactorials
A derangement is a permutation of elements where no element appears in its initial position. The subfactorial !n
can be calculated using the formula:
This alternately adds and subtracts the reciprocals of factorials, resulting in the number of derangements for a set size n
.
Subfactorial Modulo a Prime
The task of computing !n mod p
where p
is a prime number involves considering not only the derangements but also applying modular arithmetic. This interplay helps simplify expressions and manage large numbers efficiently.
Example Calculation
Let's compute !4 mod 5
.
First, calculate !4
:
• Using the formula: 4! = 24
• Hence: • Compute the sum:
Then, !4 = 24 × 0.375 = 9
.
Finally, compute 9 mod 5
:
• 9 mod 5 = 4
Thus, !4 mod 5 = 4
.
Fermat's Little Theorem and Its Role
Fermat's Little Theorem asserts that for any integer a
not divisible by a prime p
, . This property can simplify calculations in modular arithmetic. Specifically, the theorem provides a basis for simplifying powers and, consequently, helps in evaluating factorials and subfactorials modulo a prime:
If p
is a prime and n < p
, then:
•
This simplification aids computational efficiency, especially when involving large numbers.
Wilson's Theorem
Wilson’s Theorem provides that for a prime number p
, (p-1)! ≡ -1 (mod p)
. It implies:
• For n = p - 1
, !n mod p
becomes a special case considering this theorem.
Properties and Computational Considerations
- Output Periodicity: The sequence
!n mod poften shows periodic behaviors due to the repetition in modular reductions. - Computational Challenges: Large values of
nrequire efficient algorithms to compute factorials and subfactorials. Modular arithmetic mitigates overflow and reduces computational complexity. - Pre-computable Tables: For fixed primes and manageable
n, tables can be pre-computed to quickly reference!n mod p.
Table: Subfactorial Modulo Prime for Small n
and p
n | !n | p (Prime) | !n mod p | |
| 0 | 1 | 3 | 1 | |
| 1 | 0 | 3 | 0 | |
| 2 | 1 | 3 | 1 | |
| 3 | 2 | 3 | 2 | |
| 4 | 9 | 5 | 4 | |
| 5 | 44 | 7 | 2 | |
| 6 | 265 | 11 | 1 |
Conclusion
The study of subfactorials modulo a prime number accommodates significant insights into permutations and their properties, intersecting combinatorial mathematics and number theory. The use of classic theorems such as Fermat's Little Theorem and Wilson's Theorem fortifies practical implementations and fosters further exploration in both theoretical and applied contexts.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.