FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TheAlgorithms_Go/math/binomialcoefficient.go at master · cureking/TheAlgorithms_Go · GitHub
cureking
TheAlgorithms_Go
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TheAlgorithms_Go
/
math
/
binomialcoefficient.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
33 lines (29 loc) · 752 Bytes
Breadcrumbs
TheAlgorithms_Go
/
math
/
binomialcoefficient.go
Copy path
File metadata and controls
33 lines (29 loc) · 752 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// binomialcoefficient.go
// description: Returns C(n, k)
// details:
// a binomial coefficient C(n,k) gives number ways
// in which k objects can be chosen from n objects.
// wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see binomialcoefficient_test.go
package
math
import
(
"errors"
)
var
ErrPosArgsOnly
error
=
errors
.
New
(
"arguments must be positive"
)
// C is Binomial Coefficient function
// This function returns C(n, k) for given n and k
func
Combinations
(
n
int
,
k
int
) (
int
,
error
) {
if
n
<
0
||
k
<
0
{
return
-
1
,
ErrPosArgsOnly
}
if
k
>
(
n
-
k
) {
k
=
n
-
k
}
res
:=
1
for
i
:=
0
;
i
<
k
;
i
++
{
res
*=
(
n
-
i
)
res
/=
(
i
+
1
)
}
return
res
,
nil
}
Back
|
FazBrowse Home
|
New Git URL