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
+28.4k Get file path of temporary file in Go
+7.5k Golang : Command line ticker to show work in progress
+10k Golang : Use regular expression to get all upper case or lower case characters example
+7.7k Golang : Scan files for certain pattern and rename part of the files
+20k Golang : Count number of digits from given integer value
+31.4k Golang : How to convert(cast) string to IP address?
+4.9k Swift : Convert (cast) Float to Int or Int32 value
+25.2k Golang : Get current file path of a file or executable
+13.6k Generate salted password with OpenSSL example
+11.4k Golang : Handle API query by curl with Gorilla Queries example
+21.9k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+23k Golang : Randomly pick an item from a slice/array example