October 16, 2008 thumb Ackerman

Ackerman merupakan sebuah program yang mengimplementasikan fungsi rekursif…..
yang mana ia akan memiliki 3 fungsi …….

/*
This program use to implement Ackerman function
With : a(m,n)——>return(a((m-1),a(m,n-1)))
a(0,n)——>return(n+1)
a(n,0)——>return(a(m-1,1))
Author : huda
e-mail : huda890@gmail.com
*/
/*iporting Input and Output Standart from java*/
import java.io.*;
/*declaration of ackerman class*/
class ackerman
{
/*declaration of main methode*/
public static void main(String args[]) throws IOException
{
/*build object of ackerman class*/
ackerman fungsi=new ackerman();
/*make object for input from console*/
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int m,n;
String temporary;
System.out.println(“Ackerman”);
System.out.println(“a(m,n)——>a((m-1),a(m,n-1))”);
System.out.println(“a(0,n)——>n+1”);
System.out.println(“a(n,0)——>a(m-1,1)”);
System.out.print(“insert value of M:”);
/*read string from console*/
temporary=input.readLine();
/*convert string to integer*/
m=Integer.parseInt(temporary);
System.out.print(“insert value of N:”);
/*read input from console */
temporary=input.readLine();
/*convert from string to integer*/
n=Integer.parseInt(temporary);
/*call ackerman function*/
int result=fungsi.ackerman(m,n);
/*print to console the result*/
System.out.println(result);
}
/*ackerman methode */
int ackerman(int m, int n)
{
int result;
if(m==0 && n!=0)
{return(n+1);}
else
if (n==0 && m!=0)
{
result=ackerman(m-1,1);
/*return the result */
return(resultl);
}
else
if (m!=0 && n!=0)
{
result=ackerman(m,n-1); /*found the result of first function */
/*the result of the first function used to found the second result */
result=ackerman(m-1,result); /*found the final result*/
/*return the result*/
return(hasil);
}
else /*result 1 if n=0 and m=0*/
return(1);
}
}

Comments

total comments