How to Solve nCr%p?

How to Calculate the Value of nCr%p?
How to Calculate the Value of nCr%p?

Problem Statement

Examples

Brute Force Approach

nCr = n-1Cr-1 + n-1Cr
package com.Tekolio.Extras// solve nCr%ppublic class solve_cobnimatrics_1 {    public static void main(String[] args) {        int n = 5;        int r = 2;        int p = 13;       System.out.print("Value of "+n+"C"+r+" % "+p+" is "+solve(n, r, p));    }    public static int solve(int A, int B, int C) {        if (B > A) // the value of r can never be greater than n            return 0;        if (B == 0 || B == A) // 0C0 = 1 & nCn = 1            return 1;        return (solve(A - 1, B - 1, C)%C + solve(A - 1, B, C)%C)%C;    }}
;

Output

Binomial Coefficients Recursion tree for C(5,2)
Binomial Coefficients Recursion tree for C(5,2)

Read More

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ateev Duggal

I am a front-end developer from India with over one year of experience in freelancing with skills like Git, HTML5, CSS3, Bootstrap 4&5, and React.