How To Perform nCr (r-combinations) in Golang?



In this tutorial, we will perform nCr(r-combinations) in the Golang programming language. The use case of nCr(r-combinations) is to find the total number of possible arrangements where the order does not matter. In other words, we are selecting the r items from n items where the order does not matter. This tutorial will include two ways to find this in the Golang programming language.

Explanation

For example, for n =5 and r = 3

nCr = n! / ( r! * ( n - r )! )

= 5! / ( 3! * 2! )

= 120 / 12

= 10

Algorithm

Step 1 - Declare all the required variables to store n, r, n factorial, r factorial and n-r factorial.

Step 2 - Initializing the value of n, r, n factorial, r factorial and n-r factorial.

Step 3 - Finding the factorial of n, r and n-r.

Step 4 - Find nCr using the above formulae.

Step 5 - Printing the result.

Example

In this example, we are going to find nCr using a for loop.

package main

// fmt package provides the function to print anything
import (
   "fmt"
)
func main() {
   
   // declaring the variables to store the value of n, r and answer
   var n, r, nFactorial, rFactorial, nminusrFactorial, answer int
   fmt.Println("Program to find the nCr using the for loop.")
   
   // initializing the value of n 
   n = 10
   
   // initializing the value of r
   r = 8
   nFactorial = 1
   
   // finding factorial of n
   for i := 1; i <= n; i++ {
      nFactorial = nFactorial * i
   }
   rFactorial = 1
   
   // finding factorial of r
   for i := 1; i <= r; i++ {
      rFactorial = rFactorial * i
   }
   nminusrFactorial = 1
   
   // finding factorial of n - r
   for i := 1; i <= n-r; i++ {
      nminusrFactorial = nminusrFactorial * i
   }
   
   // finding answer by using the formulae
   answer = nFactorial / (rFactorial * nminusrFactorial)
   
   // printing the result
   fmt.Println("The value of nCr with n =", n, "and r=", r, "is", answer)
}

Output

Program to find the nCr using the for loop.
The value of nCr with n = 10 and r= 8 is 45

Algorithm

Step 1 - Declare all the required variables to store n, r, n factorial, r factorial and n-r factorial.

Step 2 - Initializing the value of n, r, n factorial, r factorial and n-r factorial.

Step 3 - Finding the factorial of n, r and n-r in the separate function.

Step 4 - Find nCr using the above formulae.

Step 5 - Printing the result.

Example

In this example, we are finding the nCr using the separate function to find the factorial of n, r and n-r.

package main

// fmt package provides the function to print anything
import (
   "fmt"
)

// this is a recursive function of return type int

// which is returning the factorial of number

// passed in argument
func factorial(n int) int {
   if n == 1 {
      return 1
   }
   return factorial(n-1) * n
}
func main() {
   
   // declaring the variables to store the value of n, r and answer
   var n, r, nFactorial, rFactorial, nminusrFactorial, answer int
   fmt.Println("Program to find the nCr using the separate function to find the factorial of n, r and, n-r.")
   
   // initializing the value of n
   n = 10
   
   // initializing the value of r
   r = 8
   
   // finding factorial of n
   nFactorial = factorial(n)
   
   // finding factorial of r
   rFactorial = factorial(r)
   
   // finding factorial of n - r 
   nminusrFactorial = factorial(n - r)
   
   // finding answer by using the formulae
   answer = nFactorial / (rFactorial * nminusrFactorial)
   
   // printing the result
   fmt.Println("The value of nCr with n =", n, "and r=", r, "is", answer)
} 

Output

Program to find the nCr using the separate function to find the factorial of n, r and, n-r.
The value of nCr with n = 10 and r= 8 is 45

Conclusion

These are the two ways to perform nCr(r-combinations) in the Golang programming language. The second way is much better in terms of modularity and code reusability as we can call that function anywhere in the project. To learn more about Golang you can explore these tutorials.


Advertisements