Given three integers N, R and P where P is prime, the task is to find whether NCR is divisible by P or not.
Examples:
Input: N = 6, R = 2, P = 7
Output: No
6C2 = 15 which is not divisible by 7.Input: N = 7, R = 2, P = 3
Output: Yes
7C2 = 21 which is divisible by 3.
Approach: We know that NCR = N! / (R! * (N – R)!). Now using Legendre Formula, find the largest power of P which divides any N!, R! and (N -R)! say x1, x2 and x3 respectively.
In order for NCR to be divisible by P, the condition x1 > x2 + x3 must be satisfied.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> #define ll long long int using namespace std; // Function to return the highest // power of p that divides n! // implementing Legendre Formula int getfactor(int n, int p) { int pw = 0; while (n) { n /= p; pw += n; } // Return the highest power of p // which divides n! return pw; } // Function that returns true // if nCr is divisible by p bool isDivisible(int n, int r, int p) { // Find the highest powers of p // that divide n!, r! and (n - r)! int x1 = getfactor(n, p); int x2 = getfactor(r, p); int x3 = getfactor(n - r, p); // If nCr is divisible by p if (x1 > x2 + x3) return true; return false; } // Driver code int main() { int n = 7, r = 2, p = 7; if (isDivisible(n, r, p)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java Implementation of above approach import java.io.*; class GFG { // Function to return the highest // power of p that divides n! // implementing Legendre Formula static int getfactor(int n, int p) { int pw = 0; while (n != 0) { n /= p; pw += n; } // Return the highest power of p // which divides n! return pw; } // Function to return N digits // number which is divisible by D static int isDivisible(int n, int r, int p) { // Find the highest powers of p // that divide n!, r! and (n - r)! int x1 = getfactor(n, p); int x2 = getfactor(r, p); int x3 = getfactor(n - r, p); // If nCr is divisible by p if (x1 > x2 + x3) return 1; return 0; } // Driver code public static void main (String[] args) { int n = 7, r = 2, p = 7; if (isDivisible(n, r, p) == 1) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by krikti.. |
Python3
# Python3 implementation of the approach # Function to return the highest # power of p that divides n! # implementing Legendre Formula def getfactor(n, p) : pw = 0; while (n) : n //= p; pw += n; # Return the highest power of p # which divides n! return pw; # Function that returns true # if nCr is divisible by p def isDivisible(n, r, p) : # Find the highest powers of p # that divide n!, r! and (n - r)! x1 = getfactor(n, p); x2 = getfactor(r, p); x3 = getfactor(n - r, p); # If nCr is divisible by p if (x1 > x2 + x3) : return True; return False; # Driver code if __name__ == "__main__" : n = 7; r = 2; p = 7; if (isDivisible(n, r, p)) : print("Yes"); else : print("No"); # This code is contributed by AnkitRai01 |
C#
// C# Implementation of above approach using System; class GFG { // Function to return the highest // power of p that divides n! // implementing Legendre Formula static int getfactor(int n, int p) { int pw = 0; while (n != 0) { n /= p; pw += n; } // Return the highest power of p // which divides n! return pw; } // Function to return N digits // number which is divisible by D static int isDivisible(int n, int r, int p) { // Find the highest powers of p // that divide n!, r! and (n - r)! int x1 = getfactor(n, p); int x2 = getfactor(r, p); int x3 = getfactor(n - r, p); // If nCr is divisible by p if (x1 > x2 + x3) return 1; return 0; } // Driver code static public void Main () { int n = 7, r = 2, p = 7; if (isDivisible(n, r, p) == 1) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by ajit. |
Yes
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find a pair from the given array with maximum nCr value
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
- Compute nCr % p | Set 2 (Lucas Theorem)
- Compute nCr % p | Set 3 (Using Fermat Little Theorem)
- Program to calculate value of nCr
- Sum of product of r and rth Binomial Coefficient (r * nCr)
- Queries of nCr%p in O(1) time complexity
- Program to calculate the value of nCr Efficiently
- Calculate nCr using Pascal's Triangle
- Find permutation of n which is divisible by 3 but not divisible by 6
- Find prime factors of Array elements whose sum of exponents is divisible by K
- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Count of numbers upto M divisible by given Prime Numbers
- Quick ways to check for Prime and find next Prime in Java
- Find coordinates of a prime number in a Prime Spiral
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Count all prime numbers in a given range whose sum of digits is also prime
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Check if LCM of array elements is divisible by a prime number or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

