Lucas Theorem

Learn the Lucas Theorem to calculate the binomial coefficient.

We'll cover the following

Problem introduction

Given three numbers n, r, and p, compute the above value of (nr)\binom{n}{r}% p.

Lucas theorem approach

In number theory, the Lucas’ Theorem expresses the remainder of the division of the binomial coefficient (nr)\binom{n}{r} by a prime number p in terms of base p expansions of integers n and r.

The Lucas Theorem suggests that the value of (nr)\binom{n}{r} can be computed by multiplying results of (niri)\binom{n_{i}}{r_{i}} where nin_{i} and rir_{i} are individually same-positioned digits in base p representations of n and r respectively. The idea is to one by one compute (niri)\binom{n_{i}}{r_{i}} for individual digits nin_{i} and rir_{i} in base p.

Let us now look at how this solution can be implemented.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.