How to find permutation and combination of number in Golang 1.20
In this video we are going to generate source code for the GoLang Program, How to find permutation and combination of number using chatGPT.
After generating we will verify and run the generated code.
IMPORTANT NOTE on SOURCE CODE: only source code that are acceptable by the youtube description box will be displayed.
##############SOURCE CODE#########################
package main
import (
"fmt"
"math"
)
func main() {
var n, r float64
fmt.Print("Enter the value of n: ")
fmt.Scan(&n)
fmt.Print("Enter the value of r: ")
fmt.Scan(&r)
// calculate permutation and combination
permutation := math.Gamma(n+1) / math.Gamma(n-r+1)
combination := math.Gamma(n+1) / (math.Gamma(r+1) * math.Gamma(n-r+1))
// print the results
fmt.Printf("Permutation of %.0f objects taken %.0f at a time is %.0f\n", n, r, permutation)
fmt.Printf("Combination of %.0f objects taken %.0f at a time is %.0f\n", n, r, combination)
}
##############END OF SOURCE CODE#########################
Below is the explanation for the program:
To find the permutation and combination of a number, we can use the math package in Go which provides functions to calculate permutations and combinations.
Permutation:
In mathematics, a permutation is a way to arrange a set of objects in a particular order.
The number of permutations of a set of n objects is given by n!.
Combination:
A combination is a way to select a subset of objects from a larger set, without regard to the order in which the objects are selected.
The number of combinations of n objects taken r at a time is given by nCr = n!/(r!(n-r)!).
In this program, we first ask the user to enter the values of n and r.
We then use the math.Gamma() function to calculate the factorials of n, n-r, and r, and
then use these values to calculate the permutation and combination using the formulas mentioned above.
#go #goprogramming #golang #golangtutorial #golanguage