Yesterday I came in touch with a curious, astonishing mathematical function. It’s called Ackermann function.

Mathematically speaking, it is a **well-defined total function**. That is, it has defined values for every integer input (= total function), and this value is not ambiguous (every input has one and one only possible output value) (= well-defined).

Speaking about computer science, this function is **computable**, but it’s not a **primitive recursive** function. In other words, you can implement an algorithm to express the function using **while-loops** (= computable), but still you cannot write an equivalent algorithm using only **do-loops** (= not primitive recursive). I suggest you to try this statement.

The function has this form:

As you can see, it’s form is **fairly simple**. But, even if it seems simple, its values explode quickly. *A(4, 2)*, for example, has more than **19.000 digits**.

What I want to talk about is the possible implementation of such a function by means of a computer algorithm. I will use **Python** for the next examples, but the description is language-independent.

#### Recursive approach

The quicker approach to solving such a problem is by implementing a **recursive algorithm**, since the definition of the function itself is recursive. Let’s try with something like this:

import sys def A(x, y): if x == 0: return y+1 else: if y == 0: return A(x-1, 1) else: return A(x-1, A(x, y-1)) x = int(sys.argv[1]) y = int(sys.argv[2]) print "Result of A(%d, %d) is %d" % (x, y, A(x, y))

You will want to try it calling from command line:

python ackermann.py 4 2

You will face a brutal reality: this algorithm **will not end** for the input (4, 2), since it quickly reaches the **maximum recursion depth** provided by Python. We will get better results starting from lower inputs.

You will find that the maximum computable values, for each possible x are:

- A(1, 997) = 999
- A(2, 497) = 997
- A(3, 6) = 509
- A(4, 0) = 13

For every input * (x, y)* with

*greater than 5 you will get no result, for each*

**x***. As you can see, the function quickly diverges in complexity when*

**y****, and much more when**

*x = 3***.**

*x = 4*What we can try to do is jump as much iterations as possible with a simple trick: saving already computed values in order to not compute them anymore. This could simplify the navigation through the recursion graph.

Let’s try this implementation (with some debug information):

import sys res = {} jumps = 0 recursions = 0 #------ Start Method -------# def A(x, y): global res, jumps, recursions recursions += 1 try: jumps += 1 return res[x][y] except Exception, e: jumps -= 1 res[x] = {} if x == 0: res[x][y] = y+1 else: if y == 0: res[x][y] = A(x-1, 1) else: res[x][y] = A(x-1, A(x, y-1)) return res[x][y] #------ End Method -------# x = int(sys.argv[1]) y = int(sys.argv[2]) try: print "Result of A(%d, %d) is %d" % (x, y, A(x, y)) print "%d total recursions, %d operations avoided" % (recursions, jumps) except Exception, e: print "Exception occurred after %d recursions" % (recursions)

As you can see, the res variable is loaded with each computed value (at lines 20, 23, 25), and at the beginning of the function we try to return the value of * A(x, y)* if already computed (at line 14). The jumps variable keeps track of the number of the iterations saved by this implementation, while the recursions variable keeps track of the total number of single calls the the

*function.*

**A**You can now still try to call the function to see what the maximum computable values are. You will find this:

- A(1, 995) = 997
- A(2, 496) = 995
- A(3, 6) = 509
- A(4, 0) = 13

We got three results: the maximum computable inputs have decreased ((1, 995) instead of (1,997) for example), the number of jumps increases with increasing values of the inputs, thus the computation time has decreased. Sadly, our target was not to reduce computable time, but reduce recursion depth.

We should try something different, to truly **master** the Ackermann Function. This something different involves computing the **analytic form** of the function. This approach will show a completely different world.

Probably I’ll talk about that in a **future article**. Let me know what you think about it, and **submit** your **different implementations** (different languages, different approaches) but still focusing on the **recursion optimization**.

cbd oil store [url=https://cbdhempoildk.com/ ]cbd oil for sale joy organics [/url] full spectrum cbd cbd oil near me best cbd oil for pain

This article gives clear idea in favor of the new viewers of blogging, that truly how to do blogging and site-building.

I have been absent for some time, but now I remember why I used to love this website. Thank you, I’ll try and check back more often. How frequently you update your web site?

Hi there, I enjoy reading all of your article.

I like to write a little comment to support you. https://philetfredpizzeria.com/

This info is priceless. When can I find out more?

When somekne writes an post he/shе maintains thhe

idea of a user in his/heг mind that how a usеr can bbe aware of it.

So that’s why this article is ɑmazing. Thanks! https://www.nav-bookmarks.win/harga-rumput-sintetis-termurah-2021

Hurrah! After all I got a web site from where I be capable

of really get helpful information regarding my study and knowledge.

Excellent beat ! I would like to apprentice while you amend your site, how can i subscribe for a blog site? The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear idea|

What’s up, its nice post concerning media print, we all know media is a wonderful source of data.

It’s appropriate time to make some plans for the future and it’s time

to be happy. I’ve read this post and if I could I wish to suggest you some interesting things or advice.

Maybe you can write next articles referring to this article.

I wish to read more things about it!

Hi to all, how is all, I think every one is getting more from this

web site, and your views are nice designed for new users.

Wow, this article is pleasant, my younger

sister is analyzing these things, thus I am going to convey her.

I love it when people get together and share views.

Great blog, stick with it!

Your style is really unique in comparison to other people I have read stuff from.

I appreciate you for posting when you’ve got the opportunity, Guess I will just bookmark this site.

hacklink servisleri, instagram hacklink hizmeti satın al.

Have you ever considered about adding a little bit more than just your articles?

I mean, what you say is fundamental and all. Nevertheless just imagine if you added some great

graphics or videos to give your posts more, “pop”!

Your content is excellent but with pics and video clips,

this site could undeniably be one of the very best

in its niche. Terrific blog!

I simply wanted to send a quick remark so as to express gratitude to you for all of the pleasant information you are giving on this website. My particularly long internet lookup has finally been recognized with beneficial facts to write about with my relatives. I would point out that we website visitors are definitely fortunate to be in a very good place with so many wonderful professionals with valuable advice. I feel truly happy to have used your web pages and look forward to tons of more cool minutes reading here. Thank you again for a lot of things.

I will immediately grab your rss as I can?t find your e-mail subscription link or newsletter service.

Do you have any?

Kindly allow me recognise in

order that I may subscribe.

Thanks. http://applicationmp.com

Usually I do not learn article on blogs, however I would like to say that this write-up very forced me to try and do it! Your writing taste has been amazed me. Thank you, very nice article.

That is really fascinating, You’re an overly skilled blogger.

I have joined your rss feed and look forward to seeking more of your excellent post.

Also, I have shared your site in my social networks

Attractive section of content. I just stumbled

upon your site and in accession capital to assert that

I get in fact enjoyed account your blog posts. Any way I will

be subscribing to your feeds and even I achievement you access consistently rapidly.

Hello! I know this is somewhat off topic but I was wondering which blog platform are you using for this site?

I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking at options for

another platform. I would be great if you could point me

in the direction of a good platform.

}

There is definately a lot to find out about this issue. I

like all of the points you made.

Valuable information. Lucky me I found your site by chance, and I’m shocked why this twist of fate did not happened in advance! I bookmarked it.|