Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created March 16, 2017 08:04
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/954c379a21ede28139b61c9dfee6dc4a to your computer and use it in GitHub Desktop.
Save thinkphp/954c379a21ede28139b61c9dfee6dc4a to your computer and use it in GitHub Desktop.
Pascals Triangle in Golang.
/**
*
* Adrian Statescu (http://adrianstatescu.com)
*
* Pascal's Triangle
*
* License MIT
*
*/
package main
import ("fmt"
"os"
"strconv")
func displayPascalTriangle(n int) {
var pascal [ 100 ][ 100 ]int
var i, j, k, sp 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 ]
}
}
}
sp = n
for i = 0; i <= n; i++ {
for k = sp; k >= 0; k-- {
fmt.Printf(" ")
}
sp--
for j = 0; j <= i; j++ {
fmt.Printf("%d ", pascal[i][j])
}
fmt.Printf("\n")
}
}
func main() {
var n int
n,_ = strconv.Atoi(os.Args[1])
displayPascalTriangle( n )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment