Skip to content

Instantly share code, notes, and snippets.

@jhorowitz
Last active February 14, 2016 17:14
Show Gist options
  • Save jhorowitz/ce429e1452d7c611174d to your computer and use it in GitHub Desktop.
Save jhorowitz/ce429e1452d7c611174d to your computer and use it in GitHub Desktop.
Coin sum problem solved recursively in exponential time.
package main
import "fmt"
const target = 200
var denoms = [...]int{200, 100, 50, 20, 10, 5, 2, 1}
func main() {
fmt.Println(CoinSum(0, 0))
}
func CoinSum(sum int, index int) (result int) {
if index >= len(denoms) || sum > target {
return 0
} else if sum == target {
return 1
}
result += CoinSum(sum+denoms[index], index)
result += CoinSum(sum, index+1)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment