The Ackermann function A : N 2 → N is defined by the recursive relation A ( m , n ) = ⎩ ⎪ ⎨ ⎪ ⎧ n + 1 A ( m − 1 , 1 ) A ( m − 1 , A ( m , n − 1 ) ) ( m = 0 ) ( m > 0 , n = 0 ) ( m > 0 , n > 0 ) . As an example, compute: A ( 1 , 2 ) = A ( 0 , A ( 1 , 1 ) ) = A ( 0 , A ( 0 , A ( 1 , 0 ) ) ) = A ( 0 , A ( 0 , A ( 0 , 1 ) ) ) = A ( 0 , A ( 0 , 2 ) ) = A ( 0 , 3 ) = 4 . Consider the above computation string as a raw string of characters, i.e.
1 |
|
One can see, that it has 9 7 characters. How long is the raw computation string for A ( 3 , 5 ) ?
Details and assumptions
the computation string is generated by simply using the above recursive formula (there is no remembering of the values known form dynamic programing etc.),
the arguments are separated by two characters: comma and a single space,
there is exactly one space on both sides of the equal sign,
there is no extra character at the end of the string.
Test cases
Let L ( m , n ) be the length of the raw computation string of A ( m , n ) . Then:
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Veryyyyyyyyyyyyyyyyyyyyy confusinggggggggggggggggggg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
@Piotr Idzik can you comment on the efficiency of my code i am not sure if it the most efficient and short at at same time
Log in to reply
I did a quick benchmark. Your code needs about 47.63 [s] to complete, my code requires 8.02 [s] and the code by @David Vreken needs incredible 0.17 [s].
Few remarks:
Concerning the compactness of the code: people doing code golf are incredible (take a look here for some crazy examples). I am almost sure that one can write a program solving this puzzle, which uses only "few" bytes. However, it is not always the case, that the shorted program is better. There is this saying, that the program is written once but read many times. (Slightly) Longer code can be easier to read, maintain, understand and reuse by the others. And for the computer, it does not really matter how long the code is.
Quite efficient and concise python code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
The Python program below computes L ( 3 , 5 ) = 2 7 9 6 4 5 5 9 .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
Problem Loading...
Note Loading...
Set Loading...
Below you will find my python code. As a bonus exercise, you can try to adapt it in such a way, that it will solve a similar problem for the greatest common divisor calculated with Euclid's algorithm.