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 % p.
Lucas theorem approach
In number theory, the Lucas’ Theorem expresses the remainder of the division of the binomial coefficient by a prime number p
in terms of base p
expansions of integers n
and r
.
The Lucas Theorem suggests that the value of can be computed by multiplying results of
where and are individually same-positioned digits in base p
representations of n
and r
respectively.
The idea is to one by one compute for individual digits and in base p
.
Let us now look at how this solution can be implemented.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.