Golang : Ackermann function example
Not really a tutorial, but just want to play with recursive function and toying with Ackermann function. Hope this example can be useful to you.
See http://en.wikipedia.org/wiki/Ackermann_function
package main
import (
"fmt"
"strconv"
"os"
)
func Ackermann(n, m int64) int64 {
// http://en.wikipedia.org/wiki/Ackermann_function
for n != 0 {
if m == 0 {
m = 1
} else {
m = Ackermann(n, m-1) // recursive
}
n = n - 1
}
return m + 1
}
func main() {
if len(os.Args) < 3 {
fmt.Println("[usage] : ackermann integer integer")
os.Exit(0)
}
// convert input (type string) to integer
first, err := strconv.ParseInt(os.Args[1], 10, 0)
if err != nil {
fmt.Println("First input parameter must be integer")
os.Exit(1)
}
second, err := strconv.ParseInt(os.Args[2], 10, 0)
if err != nil {
fmt.Println("Second input parameter must be integer")
os.Exit(1)
}
answer := Ackermann(first, second)
fmt.Println(answer)
}
Sample output :
./ackermnn 3
[usage] : ackermann integer integer
./ackermnn 3 2
29
References :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+17.1k Golang : Get number of CPU cores
+12.5k Golang : How to check if a string starts or ends with certain characters or words?
+12.4k Golang : Display list of countries and ISO codes
+9.6k Mac OSX : Get a process/daemon status information
+20.6k nginx: [emerg] unknown directive "passenger_enabled"
+11.8k Android Studio : Create custom icons for your application example
+5.2k Linux : How to set root password in Linux Mint
+10.8k Golang : Resolve domain name to IP4 and IP6 addresses.
+5.9k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+17.3k Golang : Capture stdout of a child process and act according to the result
+8.8k Golang : Another camera capture GUI application with GTK and OpenCV