- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find if nCr is divisible by the given prime in C++

Suppose there are three variables N, R and P. The N and R are used to get the ^{N}C_{R} and P is a prime. We have to find whether ^{N}C_{R} is divisible by P. Suppose we have some numbers N = 7, R = 2 and P = 3, then ^{7}C_{2} = 21, this is divisible by 3, so the output will be true.

We know that ^{N}C_{R} = N! / (R! * (N – R)! ). We will use Legendre Formula to largest power of P, which divides any N!, R! and (N – R)! in order to NCR to be divisible by P, the condition is N! > R! + (N - R)!

## Example

#include <iostream> using namespace std; int getPower(int n, int p) { int pow = 0; while (n) { n /= p; pow += n; } return pow; } bool isDivisibleByP(int n, int r, int p) { // Find the highest powers of p // that divide n!, r! and (n - r)! int x1 = getPower(n, p); int x2 = getPower(r, p); int x3 = getPower(n - r, p); if (x1 > x2 + x3) return true; return false; } int main() { int n = 7, r = 2, p = 7; if (isDivisibleByP(n, r, p)) cout << "nCr is divisible by P"; else cout << "nCr is not divisible by P"; }

## Output

nCr is divisible by P

- Related Questions & Answers
- Check if a number is divisible by all prime divisors of another number in C++
- Check if LCM of array elements is divisible by a prime number or not in Python
- Find if a number is divisible by every number in a list in C++
- Find permutation of n which is divisible by 3 but not divisible by 6 in C++
- Find a pair from the given array with maximum nCr value in Python
- Find a pair from the given array with maximum nCr value in C++
- C# Program to find whether the Number is Divisible by 2
- Check if a large number is divisible by 20 in C++
- How to find whether the Number is Divisible by 2 using C#?
- Check if a number is divisible by 23 or not in C++
- Check if a number is divisible by 41 or not in C++
- Find the first natural number whose factorial is divisible by x in C++
- Find N digits number which is divisible by D in C++
- Python Program to Find if a Number is Prime or Not Prime Using Recursion
- C Program find nCr and nPr.

Advertisements