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 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 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 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.
of course like your website however you have to take a look at the spelling
on several of your posts. Many of them are rife with spelling problems and I find it
very bothersome to inform the reality on the other hand I will definitely come
again again.
Wonderful, what a blog it is! This weblog presents helpful information to us,
keep it up.
I visited several ѡebsites but the audio feаturе for audio
songs present at this site is in fact fabulous. https://www.liveinternet.ru/users/gamejudicasinoio/post477439016/
You made some decent points there. I checked on the web for more information about the issue and found most people will go along with your views on this website.
This blog was… how do I say it? Relevant!!
Finally I have found something which helped me. Appreciate it!
Here is my web site Blood Irradiator Sales
of cߋurse like your web site but you need to check
thе spelling оn several off your postѕ. Several of hem are rife ѡith spelling issues and I in findіng it very bothersome
tto inform the truth then agаin I will ceгtɑinly come aցain again.
my website: pinjaman bri
Right here is the perfect website for everyone who wants to find out
about this topic. You know a whole lot its almost
hard to argue with you (not that I really would want to…HaHa).
You certainly put a brand new spin on a subject
that’s been discussed for many years. Wonderful stuff, just wonderful!
Hi, I do believe this is a great web site. I stumbledupon it 😉 I am going to return yet again since I book-marked it.
Money and freedom is the best way to change, may you be rich and continue to help others.
Tremendous things here. I am very satisfied to peer your article.
Thank you a lot and I am taking a look forward to touch you.
Will you please drop me a e-mail?
A person necessarily lend a hand to make severely
articles I would state. This is the first time I frequented your web page and
so far? I surprised with the analysis you made to make this particular publish
amazing. Wonderful task!
Hey there exceptional blog! Does running a blog similar to this take a massive amount work?
I’ve very little understanding of coding but I had been hoping to start my own blog in the near
future. Anyhow, should you have any ideas or techniques for new blog owners please share.
I know this is off topic but I simply had to ask. Thanks a lot!
Promotion for every deposit receive a 10% bonus, apply for a new member, receive a bonus of up to 50%Superslot
I was wondering if you ever thought of changing the structure of your blog?
Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect
with it better. Youve got an awful lot of text for only having 1
or 2 pictures. Maybe you could space it out better?
Also visit my blog: Blood Irradiation
jokerThe # 1 slot game is an app with a wide variety of games to choose from. Which has more than 100 items ever There are casino games to choose from, whether it is online slots, fish shooting, casino
pg auto Hot new online slots games in 2020 that will lead you into a whirlpool of playing online slots that have a completely new and unique game genre. Online access to 5 camps such as sexybaccarat, sagaming, aggaming, prettygaming, dreamgaming.
joker gaming Apply for a new member, deposit 50, get 100, and refer friends get 20% every time from the amount made by friends.
sexygame Apply for a new member, deposit 50, get 100, and refer friends get 20% every time from the amount made by friends.
In this case, the life insurance policy firm’s cash.
Here is my blog post :: marketing services for small business
Hi there јust wanted to gіve you a quick һeaɗs up.
The words in your рost seem to be running off the screen inn Chrome.
I’m not sure if tһis is a formatting issue or something to do with web
Ьroԝser compatibility but Ӏ thought I’d post to let yoս know.
The design look great thߋugh! Hope yoᥙ get the issue solved soon. Cheers http://lexlydia.net/forums/users/domingobrumfield/
An artificial USP human growth hormonal agent (somatropin).
Also visit my web blog – Total Marketing Services Lille
Authentic HGH Advantages (NewULife Hgh Gel Not Evaluated Yet).
Here is my homepage :: marketing services 2
They have products to offer or services to offer.
My webpage – service marketing definition in business
Hurraһ, that’s what I was exploring for, what a mateгial!
eҳidting here at this web sitе, thanks admin of this weeb site.
Ϝeel freе to visit my web blog :: agen bandarqq
XYGENYX, a licensing business for FDA-registered items.
My web site total marketing services sa
Do you have any video of that? I’d love to find
out some additional information.
Hey there I am so delighted I found your webpage, I
really found you by accident, while I was looking on Askjeeve for something else, Anyhow I am here now and
would just like to say thanks for a incredible post and a all round thrilling blog (I also love the theme/design),
I don’t have time to look over it all at the moment but
I have book-marked it and also included your RSS feeds, so when I
have time I will be back to read a lot more, Please do keep up the fantastic job.
It is 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 want to suggest you some interesting things or tips.
Perhaps you can write next articles referring to
this article. I wish to read even more things about it!
First of all I want to say fantastic blog! I had a quick question in which I’d like to ask if you don’t
mind. I was curious to find out how you center yourself
and clear your thoughts before writing. I’ve had difficulty clearing my mind
in getting my thoughts out there. I do take pleasure in writing however it just seems like the
first 10 to 15 minutes are wasted just trying to figure out how to
begin. Any ideas or tips? Appreciate it!
Here is my web blog: nha cai uy tin
clomid and pcos success stories https://clomidweb.com/
Generally I do not learn article on blogs, but I would like to say that this write-up very
forced me to take a look at and do it! Your writing
taste has been surprised me. Thank you, quite great article.
I got this web page from my friend who shared with me regazrding this
webb page and now this time I am browsing this site and reading very informative posts at this place.
It’s awesome to go to see this site and reading the views
of all mates concerning this paragraph, while I am also eager of getting experience.
I trսly love your ѡebsite.. Plеasant colors &
theme. Did you develop this web ѕite yourself? Please relly
baack as I’m wanting to create mү own personal webѕite and would likke to learn wһere you got this from
orr just ԝhat tthe thene is called. Many thanks!
Feel free tօo surf to my web site: kunjungi website kami
An artificial USP human development hormone (somatropin).
My page: total marketing services france
Ridiculous story there. What occurred after? Thanks!
Here is my webpage; website
XYGENYX, a licensing business for FDA-registered items.
Here is my webpage … item331555912
Enjoyed looking at this, very good stuff, appreciate it. “Whenever you want to marry someone, go have lunch with his ex-wife.” by Francis William Bourdillon.
After looking at a few of the blog articles on your website,
I honestly appreciate your technique of blogging.
I book marked it to my bookmark webpage list and will be
checking back soon. Take a look at my web site too
and tell me what you think.
I got this website from my
buddy who shared with me regarding this web site and at the moment this time I am visiting this site and reading
very informative articles here.
wonderful issues altogether, you just gained a brand new reader. What would you suggest about your post that you made a few days ago? Any certain?
Hello There. I found your blog using msn. This is a really well written article.
I’ll be sure to bookmark it and come back to
read more of your useful information. Thanks for the post.
I’ll certainly comeback.
I visited multiple blogs however the audio feature for audio songs
present at this site is genuinely fabulous.
Review my blog post; top-rated financial advisors near me
USP human growth hormonal agent (somatropin).
my blog post; marketing services total
Simply deѕire to say your article is as surprising. Tһe cⅼarity in your pοst
iѕ simply nice and i can assume you’re an expert on thіs ѕubject.
Fine with your permission allow me to grab your feed to keep up
to date with forthcoming post. Thanks a million and
pplease keep up the enjoyable work. https://mailzoom.co/forums/topic/dipastikan-tanpa-stres-untuk-menang-pada-agen-judi-bola-bonus-100/
It’s really very complicated in this full of activity life to listen news on TV, therefore I simply use world wide web for
that purpose, and obtain the latest news.
Gret аrticle, exactly what I waas looking for.
My web-site; kunjungi website kami
Heyа i’m for the fіrst time here. I found thіs bopаrd and I find It really useful & it helped me out much.I hope too give something back and
aіd others like уou helped me. https://hogwart-rpg.pl/forum/member.php?action=profile&uid=87256
I dugg some of you post as I cerebrated they were handy very useful
سایت پوکر ، بهترین سایت بازی پوکر
، سایت معتبر پوکر ، سایت پوکر پولی ، آدرس جدید سایت پوکر آنلاین
،آدرس بدون فیلتر، Poker Online Iran
This is really interesting, You’re a very skilled blogger.
I’ve joined your rss feed and look forward to seeking more of your fantastic post.
Also, I’ve shared your site in my social networks!