public

Solution written in Go for challenge 2012-05-25 on ProgrammingPraxis.com

  • Download Gist
challenge-2012-05-25.go
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
package main
 
import (
"fmt"
)
 
func main() {
fmt.Println("Challenge from: http://programmingpraxis.com/2012/05/25/ackermanns-function/")
fmt.Println("Challenge: Your task is to implement Ackermann’s function.")
fmt.Println("A(3, 4):", A(3, 4))
}
 
// Ackermann function according to http://programmingpraxis.com/2012/05/25/ackermanns-function/
func A(m, n int) (result int) {
if m == 0 {
result = n + 1
} else if m > 0 && n == 0 {
result = A(m-1, 1)
} else if m > 0 && n > 0 {
result = A(m-1, A(m, n-1))
}
 
return
}

Console output:

$ go run challenge-2012-05-25.go 
Challenge from: http://programmingpraxis.com/2012/05/25/ackermanns-function/
Challenge: Your task is to implement Ackermann’s function.
A(3, 4): 125

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.