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
+18.2k Golang : How to get hour, minute, second from time?
+9.9k Golang : Get login name from environment and prompt for password
+9k Golang : does not implement flag.Value (missing Set method)
+12.9k Golang : List objects in AWS S3 bucket
+17.3k Golang : [json: cannot unmarshal object into Go value of type]
+6.4k Golang : Warp text string by number of characters or runes example
+8k Golang : Configure Apache and NGINX to access your Go service example
+6.3k Golang : Map within a map example
+5.1k Golang : Generate Interleaved 2 inch by 5 inch barcode
+14k Golang : Get uploaded file name or access uploaded files
+14.3k Golang : Overwrite previous output with count down timer
+32.7k Golang : How to check if a date is within certain range?