Skip to content

Instantly share code, notes, and snippets.

@alecthegeek
Last active August 29, 2015 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alecthegeek/cfaefb9146193ec3ac68 to your computer and use it in GitHub Desktop.
Save alecthegeek/cfaefb9146193ec3ac68 to your computer and use it in GitHub Desktop.
Simple Ackermann function
// Niave implementation of the Ackermann function
// https://en.wikipedia.org/wiki/Ackermann_function
package main
import "fmt"
func Ackermann(m, n uint) uint {
//fmt.Printf("Called Ackermann(%d,%d) ", m, n)
if m == 0 {
return n + 1
}
if n == 0 {
return Ackermann(m-1, 1)
}
return Ackermann(m-1, Ackermann(m, n-1))
}
func main() {
// Will stop at Ackermann(4,1) because Ackerman(4,2) is 10^19728.30179583443
// see http://m.wolframalpha.com/input/?i=what+is+ackermann+4%2C2%3F&x=0&y=0
for i := uint(0); i < 5; i++ {
for j := uint(0); j < 2; j++ {
fmt.Printf("Ackermann of %d %d is %d\n", i, j, Ackermann(i, j))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment