Given two numbers n, r ( n>=r ). The task is to find the value of C(n, r) for big value of n.
Input: n = 30, r = 15 Output: 155117520 C(30, 15) is 155117520 by 30!/((30-15)!*15!) Input: n = 50, r = 25 Output: 126410606437752
Approach: A simple code can be created with the following knowledge that :
C(n, r) = [n * (n-1) * .... * (n-r+1)] / [r * (r-1) * .... * 1]
However, for big values of n, r the products may overflow, hence during each iteration we divide the current variables holding value of products by their gcd.
Below is the required implementation:
Time Complexity: O( R Log N)
Auxiliary Space: O(1)
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.
- Program to calculate value of nCr
- Calculate nCr using Pascal's Triangle
- 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
- C Program for efficiently print all prime factors of a given number
- Java Program for efficiently print all prime factors of a given number
- Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
- Compute nCr % p | Set 2 (Lucas Theorem)
- Sum of product of r and rth Binomial Coefficient (r * nCr)
- Queries of nCr%p in O(1) time complexity
- Find if nCr is divisible by the given prime
- Compute nCr % p | Set 3 (Using Fermat Little Theorem)
- Compute maximum of the function efficiently over all sub-arrays
- Efficiently merging two sorted arrays with O(1) extra space
- C program to calculate the value of nPr
- Program to calculate the value of sin(x) and cos(x) using Expansion
- Program to calculate the value of nPr
- Calculate the Discriminant Value
- Find minimum number of Log value needed to calculate Log upto N
- Efficient program to calculate e^x
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.