- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Swift Program To Perform nCr (rcombinations)
- Java Program to Perform nCr (rcombinations)
- C++ Program to Compute Combinations using Recurrence Relation for nCr
- How to perform fisher test in R?
- How to perform Friedman test in R?
- How to perform tukey HSD in base R?
- How to create a data frame with combinations of values in R?
- How to create combinations for each string values in two vectors in R?
- How to find all combinations of a vector elements without space in R?
- How to perform one sample Kolmogorov-Smirnov test in R?
- How to get the combinations for a range of values with repetition in R?
- Golang Program to Read Three Digits and Print all Possible Combinations from the Digits
- Program to find nCr values for r in range 0 to n, in an efficient way in Python
- How to perform paired t test for multiple columns in R?
- How to perform cartesian join for two data.table objects in R?
