Mastering the Ackermann Function

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.

Ackermann Function on the complex plane

Representation of the Ackermann Function on the complex plane

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
    if y == 0:
      return A(x-1, 1)
      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 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 x greater than 5 you will get no result, for each y. As you can see, the function quickly diverges in complexity when x = 3, and much more when 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
    jumps += 1
    return res[x][y]
  except Exception, e:
    jumps -= 1
    res[x] = {}

  if x == 0:
    res[x][y] = y+1
    if y == 0:
      res[x][y] = A(x-1, 1)
      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])

  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 A function.

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.

50.682 thoughts on “Mastering the Ackermann Function

  1. Woah! I’m really loving the template/theme of this website.

    It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between user friendliness and appearance.
    I must say you have done a amazing job with this. Additionally,
    the blog loads extremely fast for me on Chrome. Outstanding Blog!

  2. This is the right webpage for anybody who hopes to find out
    about this topic. You know so much its almost tough to argue with you (not that I
    actually would want to…HaHa). You definitely put a fresh spin on a subject that’s been discussed for a long time.

    Wonderful stuff, just excellent!

  3. It’s true that genetics and work ethic matter, however even essentially
    the most gifted pure will be destroyed by a mean lifter who juices heavily.
    The only approach to turn out to be a world-class lifter is to depart Nattyland.
    When you see that everyone in your weight class is more muscular than you
    regardless of being 5 inches shorter, you’ll know what’s up.
    In his article, Nuckols compares examined information to untested data.

    It wouldn’t be unrealistic to assume that generally “examined” stands for “smaller doses”.
    If you need extra proof, simply go to one of those “drug-free” meets and depend the lads who look natural.

    Ultimately, “lean roids” are just one other benefit over the pure man who
    competes in the identical class. Both, the natty
    and the roid person weigh the identical, but the latter is shorter, extra
    muscular, thicker, fuller and angrier. Many of the medicine above produce muscle thickening with out
    The diameter of the muscular tissues doesn’t enhance, but
    they appear more massive since the density improves.
    Anabloc wonders similar to Halotestin, Anavar, Winstrol,
    Methyl DHT , Trenbolone, Masteron, and Primobolan are thought-about medicine capable of
    adding energy without extra bulk.
    They need you to think that roids are a ornament, however that
    simply isn’t true. Without the medication, these males could be considerably smaller,
    weaker and fatter. Also, given the strategic significance of the protein mass, anabolic hormones have all the time dominated in unlawful
    sports doping, since anabolism is the process of. There is no approach
    to know for positive how much muscle it’s possible to build drug-free.

    Rules For Constructing Muscle With Out Getting Fat

    If you need to lift extraordinarily frequently, you can do that too as proven by bodybuilders and lifters
    following Eastern ways. It could be naive to suppose that prime quantities of testosterone don’t have a heavy influence
    on your capability to get psyched. Some people have the amazing ability to generate an infinite
    quantity of pressure due to their highly effective central nervous system.

    Hasan Piker Can Bro Down And Demolish Capitalism
    On The Identical Time

    But since the specialists say that Kuc was skinny, let’s use one other man as an example – Ed Coan. He competed in many weight lessons however did his greatest lifts as
    a 220lbs and 240lbs lifter. In addition, the above example doesn’t function a professional lifter.

    What is the point of doing so when everyone knows that
    the tested information are as pure as a shark in a bottle?
    The truth is that it takes an extraterrestrial sum of
    money to cowl all bases and even then there might be unnaturals remaining on the battlefield.

    Broke all plateaus and elevated my PRs by HUGE amounts.
    B1 at Its B1 right here, i think when i said i had a 130KG squat i cheated slightly which means i didn’t go below
    parallel around that point i had a video of me squatting 125KG for 3 heavy
    singles under parallel.

    Are Hormones Or Steroids Added To Chicken?

    One may say that those individuals have really sturdy CPUs that can overclock to spectacular frequencies.
    It cannot do something without authorization from the top.

    Testosterone And Penis Measurement

    One of the mind’s roles is to perform as a central processing unit .
    Steroids could have a positive effect on the entire above besides the skeletal construction of the lifter which can’t change no matter workout
    routines and steroid intake.

    Anadrole legal steroids by Crazy Bulk is among the most
    conspicuous steroid alternatives and contains Tribulus Terrestris
    as a key ingredient in boosting testosterone.

    Tribulus Terrestris is also thought-about some of the dependable and
    handiest nutritional supplements for bettering performance and

    Dianabol is well known as one of the strongest anabolic steroids on the market right now,
    delivering important will increase in energy and measurement to bodybuilders around the globe.

    Not surprisingly, it’s also considered a vital male enhancer.

    It has been studied by numerous scientists and researchers since the Seventies and proves repeatedly that
    its testosterone strengthens the skills.

    37yo male right here, I’ve been persistently lifting since age
    18. That’s 19 years of pure lifting, with breaks of 1 – 2
    months every year.
    You can’t actually know this percentage because precise naturals not
    often struggle towards roid users. Anabolic medicine have a constructive
    impact on nutrient partitioning. Hormonized lifters can eat greater than naturals as
    a result of the extra nutrients lead to muscular progress somewhat than an accumulation of adipose tissue.
    The military of hormones inside the druggies is triggering supreme synthesis of protein.

    Steroids velocity up your recovery by increasing the body’s
    anabolism. Subsequently, every routine turns into more effective when you’re on medicine.
    If you wish to practice occasionally, you are
    able to do so as proven by the American powerlifters
    from the 70s.

    Our Top 6 Greatest Legal Steroids

    I’m 38 years old and have been training as a natty since I was 15.
    I look nice, after all I needed to be greater and stronger,
    however it is what it’s. I’m aware I’ll by no means achieve what the
    faux gurus and youtubers what us to imagine is possible.
    Rather than get discouraged, embrace your strengths and practice the crap out of your “good” carry, whereas attempting to a minimum of
    hit the “common” mark on the other two.
    The power features was by far and enormous the most important eye opener.
    After about week 6 all my lifts just shot trough the roof.

    An elite powerlifter will rarely weigh lower than 240lbs at 6’1″. John Kuc set records at that weight and top, however males like Marty Gallagher considered him underweight. If each practice exhausting, the heavier and extra muscular man will obliterate the pure. The top of the lifter influences his weight class. The taller the individual, the heavier he must be to take full benefit of his leverages and carry big weights. Someone who’s 6’1″ has
    no business competing within the 135lbs category as an example.

    Here is my web blog :: arnold schwarzenegger and steroids (Alina)

  4. I think this is among the most important information for me.
    And i am glad reading your article. But wanna remark
    on few general things, The site style is great, the articles is really excellent :
    D. Good job, cheers

  5. Hey! I know this is kinda off topic but I’d figured I’d ask.

    Would you be interested in exchanging links or maybe guest writing
    a blog post or vice-versa? My site covers a lot of the same
    topics as yours and I believe we could greatly benefit
    from each other. If you are interested feel free to send me an e-mail.
    I look forward to hearing from you! Superb blog by
    the way!

  6. Hey! Quick question that’s totally off topic. Do you know
    how to make your site mobile friendly? My site looks weird when browsing from my iphone.
    I’m trying to find a theme or plugin that might be able to resolve this problem.
    If you have any suggestions, please share.


  7. I’m amazed, I must say. Seldom do I encounter a
    blog that’s equally educative and entertaining, and let
    me tell you, you have hit the nail on the
    head. The problem is something too few folks are speaking intelligently about.
    Now i’m very happy I stumbled across this during
    my search for something regarding this.

  8. Everything posted made a bunch of sense. However, think on this, suppose you added a little information? I am not saying your content is
    not good., however suppose you added something
    that grabbed a person’s attention? I mean Mastering the Ackermann Function –
    marzapower is kinda vanilla. You should glance at Yahoo’s front page and watch how they create article titles to grab people interested.
    You might try adding a video or a pic or two to grab people excited about what you’ve
    written. In my opinion, it might bring your posts a little bit more interesting.

  9. Hello There. I found your blog using msn. This is a very well written article.

    I will make sure to bookmark it and return to read more of your useful information. Thanks for the post.
    I will certainly comeback.

  10. I loved as much as you will receive carried out right here.
    The sketch is attractive, your authored subject matter stylish.

    nonetheless, you command get got an edginess over that you wish
    be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this hike.

  11. I also offer filthy gay sex texts & videos

    • 100 text replies for £10
    • Personaliseg>Vidg> – £50
    • Prerecordng>Ving> – £10*

    Any unused texts will be carried forward to your next text session or used towards
    a verbng>sexcng>

    Text me on 07854 146783

    *notice required

  12. Pretty nice post. I just stumbled upon your weblog and wanted to say that I’ve really enjoyed browsing your blog posts.
    In any case I will be subscribing to your feed and I hope you write again very

  13. Hello there, just became aware of your blog through Google, and found that it
    is really informative. I’m going to watch out for brussels.
    I will be grateful if you continue this in future. Lots
    of people will be benefited from your writing.

  14. Hellⲟ there! Τhis blog post ⅽould not be ԝritten mսch
    bеtter! Going through this article reminds me
    of my ⲣrevious roommate! Нe continually қept talking about tһis.

    I ѡill ѕend this post to hіm. Pretty sure he’ѕ gοing tо haνe a great гead.

    Tһanks for sharing!

  15. Hmm it looks like your blog ate my first comment (it was extremely long)
    so I guess I’ll just sum it up what I submitted and say,
    I’m thoroughly enjoying your blog. I too am an aspiring blog writer but I’m still new to
    everything. Do you have any recommendations for
    novice blog writers? I’d definitely appreciate it.

    my blog :: website

  16. Wonderful blog! I found it while searching on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!

  17. I loved as much as you will receive carried out
    right here. The sketch is attractive, your authored subject matter stylish.

    nonetheless, you command get bought an edginess over that
    you wish be delivering the following. unwell unquestionably come more formerly again since exactly the same
    nearly a lot often inside case you shield this hike.

    my web blog … website

  18. Attractive element 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’ll be subscribing to your feeds and even I fulfillment
    you get right of entry to persistently quickly.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *