Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created March 16, 2017 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkphp/c0dbddb57fc12d2d59bbfcc2dc480eb8 to your computer and use it in GitHub Desktop.
Save thinkphp/c0dbddb57fc12d2d59bbfcc2dc480eb8 to your computer and use it in GitHub Desktop.
Combinations using Pascal's Triangle.
/**
*
* Adrian Statescu (http://adrianstatescu.com)
*
* Comb using Pascal's Triangle
*
* License MIT
*
*/
package main
import ("fmt"
"os"
"strconv")
func comb(n int, k int) int {
var pascal [ 500 ][ 500 ]int
var i, j int
pascal[ 0 ][ 0 ] = 1
for i = 1; i <= n; i++ {
for j = 0; j <= i; j++ {
if 0 == j || j == i {
pascal[ i ][ j ] = 1
} else {
pascal[ i ][ j ] = pascal[ i - 1 ][ j ] + pascal[ i - 1 ][ j - 1 ]
}
}
}
return pascal[ n ][ k ]
}
func main() {
var n, k int
n,_ = strconv.Atoi(os.Args[1])
k,_ = strconv.Atoi(os.Args[2])
fmt.Printf("Comb(%d, %d) = %d", n, k,comb(n, k))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment