How to resolve the algorithm Ackermann function step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ackermann function step by step in the Wren programming language

Table of Contents

Problem Statement

The Ackermann function is a classic example of a recursive function, notable especially because it is not a primitive recursive function. It grows very quickly in value, as does the size of its call tree.

The Ackermann function is usually defined as follows:

Its arguments are never negative and it always terminates.

Write a function which returns the value of

A ( m , n )

{\displaystyle A(m,n)}

. Arbitrary precision is preferred (since the function grows so quickly), but not required.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Ackermann function step by step in the Wren programming language

Source code in the wren programming language

// To use recursion definition and declaration must be on separate lines
var Ackermann 
Ackermann = Fn.new {|m, n|
    if (m == 0) return n + 1
    if (n == 0) return Ackermann.call(m - 1, 1)
    return Ackermann.call(m - 1, Ackermann.call(m, n - 1))
}

var pairs = [ [1, 3], [2, 3], [3, 3], [1, 5], [2, 5], [3, 5] ]
for (pair in pairs) {
    var p1 = pair[0]
    var p2 = pair[1]
    System.print("A[%(p1), %(p2)] = %(Ackermann.call(p1, p2))")
}

  

You may also check:How to resolve the algorithm Roots of unity step by step in the REXX programming language
You may also check:How to resolve the algorithm Metronome step by step in the Nim programming language
You may also check:How to resolve the algorithm URL decoding step by step in the AWK programming language
You may also check:How to resolve the algorithm Gray code step by step in the VBScript programming language
You may also check:How to resolve the algorithm Combinations step by step in the CoffeeScript programming language