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
+22.7k Golang : Change file read or write permission example
+49.6k Golang : Check if item is in slice/array
+7.1k Golang : Intercept and compare HTTP response code example
+9.6k Golang : Find and draw contours with OpenCV example
+8.1k Golang : interface - when and where to use examples
+10k Golang : Save webcamera frames to video file
+7.8k Golang : Get all countries currencies code in JSON format
+5.7k Golang : Get environment variable
+8.6k Golang : Text file editor (accept input from screen and save to file)
+11.1k Golang : Increment string example
+3.8k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+8.9k Golang : Meaning of omitempty in struct's field tag