1) Display a trace of the intermediate values for M and N for Ackermann function
ID: 642247 • Letter: 1
Question
1) Display a trace of the intermediate values for M and N for Ackermann function when M = 2, N = 2, and for M = 3, N = 2. A trace of Ackermann's function for M = 3, N = 2, requires about 541 calls to the method. The functional value is 29. A trace of M = 2, and N = 2, requires about 27 function calls and has a value of 7.
2) Develop a table look-up method that shortens the process of computing the function values by storing intermediate results for Ackermann and using these values when they are available. Display the number of "hits" to the table look-up when doing the table look-up method.
Explanation / Answer
def ackermann_function(m, n)
raise ArgumentError, 'Must be positive integers' unless m >= 0 && n >= 0
if m == 0
n + 1
elsif n == 0
ackermann_function(m - 1, 1)
else
ackermann_function(m - 1, ackermann_function(m, n - 1))
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.