-1

我在计算Knuth 的箭头符号时遇到了问题,它是 ↑,可以函数中找到。到目前为止我所做的是:

int arrowCount = (int)arrowNum.Value; // Part of
BigInteger a = (int)aNum.Value;       // the input I
BigInteger b = (int)bNum.Value;       // already have
BigInteger result = a;
BigInteger temp = a;
for(int i = 0; i < arrowCount; i++)
{
    result = Power(temp, b);
    temp = r;
    b = a;
}

有力量

BigInteger Power(BigInteger Base, BigInteger Pow)
    {
        BigInteger x = Base;
        for(int i = 0; i < (Pow-1); i++)
        {
            x *= Base;
        }
        return x;
    }

但它的值不正确,我无法找到解决它的方法。它可以处理 1 个箭头问题,例如3↑3(即3^3 = 9),但它不能处理更多的箭头。

我需要一种方法来找出更多箭头,例如3↑↑3

应该是7625597484987 (3^27)我得到19683 (27^3)。如果您能帮助我弄清楚如何获得正确的输出并解释我做错了什么,我将不胜感激。

4

3 回答 3

2

我用java写的,输入参数使用double:

    private static double knuthArrowMath(double a, double b, int arrowNum)
{
    if( arrowNum == 1)
        return Math.pow(a, b);
    double result = a;
    for (int i = 0; i < b - 1; i++)
    {
        result = knuthArrowMath(a, result, arrowNum - 1);
    }
    return result;
}
于 2016-06-24T05:59:18.990 回答
1

如果您期望 7625597484987 (3^ 27 ) 但得到 19683 ( 27 ^3),那么在调用幂函数时交换参数不是一件简单的事情吗?

查看您的 Power 函数,您的代码片段似乎以temp为基础和b为 power 调用 Power:

int arrowCount = (int)arrowNum.Value; // Part of
BigInteger a = (int)aNum.Value;       // the input I
BigInteger b = (int)bNum.Value;       // already have
BigInteger result = a;
BigInteger temp = a;
for(int i = 0; i < arrowCount; i++)
{
    result = Power(temp, b);
    temp = result;
    b = a;
}

不应该交换 temp 和 bresult = Power(b, temp)以获得所需的结果吗?

因此,传递 1 个结果调用Power(3, 3)导致temp = 27并传递 2 个调用Power(3, 27)。它现在仅适用于单箭头的原因是因为交换第一次Power(base, power)调用的参数并不重要。

正如您在回答中指出的那样,这并不涵盖所有情况。鉴于您提供的示例,我创建了这个小控制台应用程序:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Arrow(3, 3));
            Console.WriteLine(Arrow(4, 4, 1));
            Console.WriteLine(Arrow(3, 4, 1));
            Console.ReadKey();
        }

        private static BigInteger Arrow(BigInteger baseNumber, BigInteger arrows)
        {
            return Arrow(baseNumber, baseNumber, arrows-1);
        }

        private static int Arrow(BigInteger baseNumber, BigInteger currentPower, BigInteger arrows)
        {
            Console.WriteLine("{0}^{1}", baseNumber, currentPower);
            var result = Power(baseNumber, currentPower);

            if (arrows == 1)
            {
                return result;
            }
            else
            {
                return Arrow(baseNumber, result, arrows - 1);
            }
        }

        private static BigInteger Power(BigInteger number, BigInteger power)
        {
            int x = number;
            for (int i = 0; i < (power - 1); i++)
            {
                x *= number;
            }
            return x;
        }
    }
于 2016-06-24T05:27:12.073 回答
0

我想出了一种使用该BigInteger.Pow()功能的方法。它可能看起来有点奇怪,但那是因为 C#BigInterger.Pow(x, y)只接受 y 的 int,并且 teterations 有巨大的指数。对于这种特定情况,我不得不“翻转脚本”并转换 x^y = y^x。我没有添加任何错误检查,它希望所有数字都是正整数。

知道这适用于 x^^2 和 x^^3。我也知道它适用于 2^^4 和 2^^5。我没有计算能力/内存/数学知识来知道它是否适用于任何其他数字。2^^4 和 2^^5 是我唯一可以检查和测试的。它可能适用于其他数字,但我无法确认。

int baseNum = 4;
int exp = 3;
// this example is 4^^3
BigInteger bigAnswer = tetration(baseNum, exp);

// Here is what the method that "does the work" looks like.  

    // This looks a little odd but that is because I am using BigInteger.Pow(x,y)
    // Unfortunately, y can only be an int.  Tetrations have huge exponents, so I had to figure out a
    // way to have x^y work as y^x for this specific application
    // no error checking in here, and it expects positive ints only
    // I *know* this works for x^^2, x^^3, but I don't know if it works for
    // any other number than 2 at ^^4 or higher
    public static BigInteger tetration(int baseNum, int exp)
    {
        if (exp > 2)
        {
            exp = (int)Math.Pow(baseNum, (exp - 3));
        }
        else
        {
            exp = exp - 2;
        }

        Func<BigInteger, int, BigInteger> bigPowHelper = (x, y) => BigInteger.Pow(x, y);
        BigInteger bigAnswer = baseNum;

        for (int i = 0; i < Math.Pow(baseNum, exp); i++)
        {
            bigAnswer = bigPowHelper(bigAnswer, baseNum);
        }
        return bigAnswer;
    }
于 2020-10-04T08:40:49.730 回答