汗,Ackerman函数......

来源:互联网 发布:网络不安全事件 编辑:程序博客网 时间:2024/04/29 19:27

    数据结构书的习题中看到的,Ackerman函数,它需要两个自然数作为输入值,输出一个自然数。它的输出值增长速度非常高,仅是对于(4,3)的输出已大得不能准确计算。而其反函数的增速奇慢。

环境:Vim + GCC ,Win7 32.

#include <stdio.h>/* * * * * * * * * * * * * * * * * * *  * Ackerman函数的递归实现。这个函数的 * 特性是:即使很小的 m 和 n , 他的增长 * 也很迅速,因此得到广泛研究。 * * * * * * * * * * * * * * * * * * */int Ackerman(int m, int n){    /*if (m < 0 || (m != 0 && n < 0) )    {        puts("ERROR.");        exit(-1);    }*/    if (0 == m)    {        return (n + 1);    }    if (m > 0 && 0 == n)    {        return Ackerman(m - 1, 1);    }    return Ackerman(m - 1, Ackerman(m, n -1) );}int main(void){    int m = 0, n = 0;    int result;    puts("    Ackerman Function");    printf("Input the m:");    scanf("%d", &m);    printf("Input the n:");    scanf("%d", &n);    result = Ackerman(m, n);    printf("m = %d, n = %d, result = %d.\n", m, n, result);    getch();    return 0;}

 

    实际上当m = 4,n = 1 时,就栈溢出而被系统终止了,没法正常运行,或许用迭代的方法好一些。手工计算没一会儿我就投降了,我真心佩服想出这么个函数的人啊!

 

程序开始,192 K

一直在增长,直到2200 K左右 结程序束

 

原创粉丝点击