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
+8.6k Golang : On lambda, anonymous, inline functions and function literals
+15.3k Golang : ROT47 (Caesar cipher by 47 characters) example
+19.8k Golang : How to get time from unix nano example
+8.9k Golang : How to capture return values from goroutines?
+13.8k Golang : Compress and decompress file with compress/flate example
+9.7k Golang : ffmpeg with os/exec.Command() returns non-zero status
+4.6k JavaScript: Add marker function on Google Map
+5.9k Golang : Convert Chinese UTF8 characters to Pin Yin
+5.3k Golang : fmt.Println prints out empty data from struct
+7.6k Golang : get the current working directory of a running program
+19.4k Golang : Example for DSA(Digital Signature Algorithm) package functions
+5.6k Swift : Get substring with rangeOfString() function example