阿克曼函数的定义如下:
A c k ( m , n ) = { 当 m = 0 时 返 回 n + 1 当 n = 0 时 返 回 A c k ( m − 1 , 1 ) 当 m , n > 0 时 返 回 A c k ( m − 1 , A c k ( m , n − 1 ) ) Ack(m,n)=\left\{ \begin{aligned} &当m=0时返回n+1\\ &当n=0时返回Ack(m-1,1)\\ &当m,n>0时返回Ack(m-1,Ack(m,n-1))\\ \end{aligned} \right. Ack(m,n)=⎩⎪⎨⎪⎧当m=0时返回n+1当n=0时返回Ack(m−1,1)当m,n>0时返回Ack(m−1,Ack(m,n−1))
A c k ( 0 , n ) = n + 1 A c k ( 1 , n ) = A c k ( 0 , A c k ( 1 , n − 1 ) ) = A c k ( 0 , A c k ( 0 , A c k ( 1 , n − 2 ) ) ) = A c k ( 0 , A c k ( 0 , A c k ( 0 , A c k ( 1 , n − 3 ) ) ) ) = . . . . . . = A c k ( . . . , A c k ( 1 , 0 ) . . . ) = A c k ( . . . , A c k ( 0 , 1 ) . . . ) = . . . . . . = n + 2 A c k ( 2 , n ) = A c k ( 1 , A c k ( 2 , n − 1 ) ) = A c k ( 1 , A c k ( 1 , A c k ( 2 , n − 2 ) ) ) = A c k ( 1 , A c k ( 1 , A c k ( 1 , A c k ( 2 , n − 3 ) ) ) ) = . . . . . . = A c k ( . . . , A c k ( 2 , 0 ) . . . ) = A c k ( . . . , A c k ( 1 , 1 ) . . . ) = . . . . . . = 2 n + 3 A c k ( 3 , n ) = A c k ( 2 , A c k ( 3 , n − 1 ) ) = A c k ( 2 , A c k ( 2 , A c k ( 3 , n − 2 ) ) ) = A c k ( 2 , A c k ( 2 , A c k ( 2 , A c k ( 3 , n − 3 ) ) ) ) = . . . . . . = A c k ( . . . , A c k ( 3 , 0 ) . . . ) = A c k ( . . . , A c k ( 2 , 1 ) . . . ) = . . . . . . = 2 n + 3 − 3 A c k ( 4 , n ) = A c k ( 3 , A c k ( 4 , n − 1 ) ) = A c k ( 3 , A c k ( 3 , A c k ( 4 , n − 2 ) ) ) = A c k ( 3 , A c k ( 3 , A c k ( 3 , A c k ( 4 , n − 3 ) ) ) ) = . . . . . . = A c k ( . . . , A c k ( 4 , 0 ) . . . ) = A c k ( . . . , A c k ( 3 , 1 ) . . . ) = . . . . . . = ( 有 n + 3 个 2 ) 2 2 2 2 . . . 2 − 3 Ack(0,n)=n+1\\Ack(1,n)=Ack(0,Ack(1,n-1))\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(0,Ack(0,Ack(1,n-2)))\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(0,Ack(0,Ack(0,Ack(1,n-3))))\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=......\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(...,Ack(1,0)...)\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(...,Ack(0,1)...)\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=......\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=n+2\\Ack(2,n)=Ack(1,Ack(2,n-1))\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(1,Ack(1,Ack(2,n-2)))\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(1,Ack(1,Ack(1,Ack(2,n-3))))\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=......\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(...,Ack(2,0)...)\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(...,Ack(1,1)...)\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=......\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=2n+3\\Ack(3,n)=Ack(2,Ack(3,n-1))\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(2,Ack(2,Ack(3,n-2)))\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(2,Ack(2,Ack(2,Ack(3,n-3))))\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=......\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(...,Ack(3,0)...)\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(...,Ack(2,1)...)\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=......\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=2^{n+3}-3\\Ack(4,n)=Ack(3,Ack(4,n-1))\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(3,Ack(3,Ack(4,n-2)))\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(3,Ack(3,Ack(3,Ack(4,n-3))))\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=......\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(...,Ack(4,0)...)\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=Ack(...,Ack(3,1)...)\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=......\\\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space\space=(有n+3个2)2^{2^{2^{2^{^{...^2}}}}}-3 Ack(0,n)=n+1Ack(1,n)=Ack(0,Ack(1,n−1)) =Ack(0,Ack(0,Ack(1,n−2))) =Ack(0,Ack(0,Ack(0,Ack(1,n−3)))) =...... =Ack(...,Ack(1,0)...) =Ack(...,Ack(0,1)...) =...... =n+2Ack(2,n)=Ack(1,Ack(2,n−1)) =Ack(1,Ack(1,Ack(2,n−2))) =Ack(1,Ack(1,Ack(1,Ack(2,n−3)))) =...... =Ack(...,Ack(2,0)...) =Ack(...,Ack(1,1)...) =...... =2n+3Ack(3,n)=Ack(2,Ack(3,n−1)) =Ack(2,Ack(2,Ack(3,n−2))) =Ack(2,Ack(2,Ack(2,Ack(3,n−3)))) =...... =Ack(...,Ack(3,0)...) =Ack(...,Ack(2,1)...) =...... =2n+3−3Ack(4,n)=Ack(3,Ack(4,n−1)) =Ack(3,Ack(3,Ack(4,n−2))) =Ack(3,Ack(3,Ack(3,Ack(4,n−3)))) =...... =Ack(...,Ack(4,0)...) =Ack(...,Ack(3,1)...) =...... =(有n+3个2)2222...2−3
接下来的 A c k ( m , n ) Ack(m,n) Ack(m,n)就以此类推。
假如让你用python或C++或Java编写,你会吗?
下面是参考答案(均用递推方式):
#函数部分
def Ack(m,n):
if m==0:
return n+1
elif n==0:
return Ack(m-1,1)
else:
return Ack(m-1,Ack(m,n-1))
#输入部分
m=int(input(""))
n=int(input(""))
print(Ack(m,n))
#include
using namespace std;
//函数部分
long long Ack(int m,int n){
if(m==0){
return n+1;
}else if(n==0){
return Ack(m-1,1);
}else{
return Ack(m-1,Ack(m,n-1))
}
}
//main部分
int main(){
int m,n;
cin>>m>>n;
cout<<Ack(m,n)<<endl;
return 0;
}
import java.util.Scanner;
public class Ack{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
System.out.println(Ackerman(n, m));
}
public static int Ackerman(int n,int m){
if(n==0 && m>=0)return 1;
if(n==1 && m==0)return 2;
if(n>=2 && m==0)return n+2;
if(n>=1 && m>=1)
return Ackerman(Ackerman(n-1,m),m-1);
return -1;
}
}
今天我们知道了阿克曼函数的定义和规律,还写了python和C++以及java的程序。