<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FireTeam &#187; ackermann</title>
	<atom:link href="http://www.fireteam.it/tag/ackermann/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fireteam.it</link>
	<description>#fuoco@AzzurraNet</description>
	<lastBuildDate>Tue, 16 Mar 2010 15:55:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Implementazione della funzione di Ackermann</title>
		<link>http://www.fireteam.it/2009/11/implementazione-della-funzione-di-ackermann/</link>
		<comments>http://www.fireteam.it/2009/11/implementazione-della-funzione-di-ackermann/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 15:55:55 +0000</pubDate>
		<dc:creator>saverio</dc:creator>
				<category><![CDATA[Programmazione Java]]></category>
		<category><![CDATA[ackermann]]></category>
		<category><![CDATA[calcolabilità]]></category>

		<guid isPermaLink="false">http://www.fireteam.it/?p=734</guid>
		<description><![CDATA[La funzione di Ackermann è una funzione f(x,y,z) che ha come dominio l&#8217;insieme delle terne di numeri naturali e come codominio i numeri naturali.

Essa è un esempio di funzione ricorsiva che non è primitiva ricorsiva poiché cresce più velocemente di qualsiasi funzione ricorsiva primitiva.
Qui il codice java che implementa questa funzione:

Ackermann.java

0
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
32
33
34
35
36
37
38
39
public class Ackermann &#123;
 
 static long [...]]]></description>
			<content:encoded><![CDATA[<p>La funzione di Ackermann è una funzione f(x,y,z) che ha come dominio l&#8217;insieme delle terne di numeri naturali e come codominio i numeri naturali.</p>
<p style="text-align: center;"><img class="size-full wp-image-736 aligncenter" title="ackermann" src="http://www.fireteam.it/wp-content/uploads/2009/11/ackermann.png" alt="ackermann" width="477" height="87" /></p>
<p>Essa è un esempio di funzione ricorsiva che non è primitiva ricorsiva poiché cresce più velocemente di qualsiasi funzione ricorsiva primitiva.</p>
<p>Qui il codice java che implementa questa funzione:</p>
<p><span id="more-734"></span></p>
<p><span style="text-decoration: underline;">Ackermann.java</span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
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
32
33
34
35
36
37
38
39
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Ackermann <span style="color: #009900;">&#123;</span>
 
 <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">long</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
 
 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">long</span> h<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> m, <span style="color: #000066; font-weight: bold;">long</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  
  count<span style="color: #339933;">++;</span>
  
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>m <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">return</span> n <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">return</span> h<span style="color: #009900;">&#40;</span>m<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  
  <span style="color: #000000; font-weight: bold;">return</span> h<span style="color: #009900;">&#40;</span>m<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span>, h<span style="color: #009900;">&#40;</span>m,n<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 
 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">long</span> ack<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> h<span style="color: #009900;">&#40;</span>n, n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>args.<span style="color: #006633;">length</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;usage: Ackermann &lt;number&gt;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>where number is a positive integer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  
  <span style="color: #000066; font-weight: bold;">long</span> n <span style="color: #339933;">=</span> <span style="color: #003399;">Long</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  
  count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">long</span> retValue <span style="color: #339933;">=</span> ack<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ack(&quot;</span> <span style="color: #339933;">+</span> n <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;) = &quot;</span> <span style="color: #339933;">+</span> retValue <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; [recursive calls = &quot;</span><span style="color: #339933;">+</span> count <span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  
  <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Il meccanismo di calcolo della funzione è estremamente semplice quanto pesante dal punto di vista computazionale. Questi sono i risultati ottenuti:</p>
<ul>
<li>ack(0) = <strong>1</strong>  [recursive calls = <strong>1</strong>]</li>
<li>ack(1) = <strong>3</strong>  [recursive calls = <strong>4</strong>]</li>
<li>ack(2) = <strong>7</strong>  [recursive calls = <strong>27</strong>]</li>
<li>ack(3) = <strong>61</strong> [recursive calls = <strong>2432</strong>]</li>
</ul>
<p>Non riesco a calcolare ack(4) causa stack overflow (Exception in thread &#8220;main&#8221; java.lang.StackOverflowError).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fireteam.it/2009/11/implementazione-della-funzione-di-ackermann/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
