Skip to content

Instantly share code, notes, and snippets.

@NicolaM94
Created July 29, 2023 16:31
Show Gist options
  • Save NicolaM94/df7b4551a4e737a875a11b9d406fb35f to your computer and use it in GitHub Desktop.
Save NicolaM94/df7b4551a4e737a875a11b9d406fb35f to your computer and use it in GitHub Desktop.
Pascal's triangles algorithm based on an iterative aproach
func pascalTriangleIterative(row int) []int {
if row == 1 {
return []int{1}
}
if row == 2 {
return []int{1, 1}
}
out := []int{1, 1}
for n := 3; n <= row; n++ {
temp := []int{1}
for i, j := range out[:len(out)-1] {
temp = append(temp, j+out[i+1])
}
temp = append(temp, 1)
out = temp
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment