【问题标题】:Making a recursive function iterative使递归函数迭代
【发布时间】:2019-04-03 18:28:14
【问题描述】:

作业:根据公式计算an

  • a0 = 1
  • a1 = 3
  • a2 = 5
  • an = an-1 * a2n-2 * a3 n-3

我无法使函数迭代。我想出了如何递归地做到这一点。我将如何专门为这项任务做这件事,而且一般来说?

我的递归代码:

public static BigInteger recurs(int bigInteger){
    BigInteger sum;

    if (bigInteger == 0) {
        sum = new BigInteger(String.valueOf("1"));
    } else if (bigInteger == 1) {
        sum = new BigInteger(String.valueOf("3"));
    } else if (bigInteger == 2) {
        sum = new BigInteger(String.valueOf("5"));
    } else {
        sum = recurs(bigInteger-1).multiply(recurs(bigInteger-2).pow(2).multiply(recurs(bigInteger-3).pow(3)));
    }

    return sum;

}

【问题讨论】:

  • 您能否将实际问题包含为文本而不是链接?而且,您自己尝试解决它。谢谢。
  • 我建议您在math.stackexchange.com 上提问。你可能会在那里得到更好的答案,因为这实际上只是一个伪装成编程问题的数学问题;)
  • 会尝试的。谢谢!
  • 我会使用一个包含三个变量的循环,并调用值product,它不是一个总和。顺便说一句,作为一个循环,这将 更快
  • 顺便说一句 String.valueOf(string)string

标签: java math recursion iteration


【解决方案1】:

您必须将最后三个结果存储在三个变量中,并将公式应用于这些变量。您可以在下面找到使用int 的简化示例。您可以使用BigInteger 增强此代码,因此它也适用于更大的数字。

static int compute_iterative(int n) {
    if (n == 0) return 1;
    if (n == 1) return 3;
    if (n == 2) return 5;

    int a_n3 = 1;
    int a_n2 = 3;
    int a_n1 = 5;
    int a_n = a_n1;
    int i = 3;
    while (i <= n) {
        a_n = a_n1 * (int) Math.pow(a_n2, 2) * (int) Math.pow(a_n3, 3);
        a_n3 = a_n2;
        a_n2 = a_n1;
        a_n1 = a_n;
        i++;
    }
    return a_n;
}

使用BigInterger的版本:

static BigInteger compute_iterative(int n) {
    if (n < 0) {
        throw new IllegalArgumentException("Unsupported input value: " + n);
    }
    final BigInteger[] values = { BigInteger.valueOf(1), BigInteger.valueOf(3), BigInteger.valueOf(5) };
    if (n < values.length) {
        return values[n];
    }
    int i = 3;
    while (i <= n) {
        final BigInteger result = values[2].multiply(values[1].pow(2)).multiply(values[0].pow(3));
        values[0] = values[1];
        values[1] = values[2];
        values[2] = result;
        i++;
    }
    return values[2];
}

【讨论】:

    【解决方案2】:

    您需要记住最后三个值,并根据最后一个值每次计算一个新值。

    public static BigInteger iter(int n) {
        BigInteger a = BigInteger.valueOf(1);
        BigInteger b = BigInteger.valueOf(3);
        BigInteger c = BigInteger.valueOf(5);
        switch (n) {
            case 0: return a;
            case 1: return b;
            case 2: return c;
            default:
                for (int i = 2; i < n; i++) {
                    BigInteger next = c.multiply(b.pow(2)).multiply(a.pow(3));
                    a = b;
                    b = c;
                    c = next;
                }
                return c;
        }
    }
    

    注意这是 O(n) 而不是 O(n^3)

    【讨论】:

    • 现在以封闭形式解决它,所以它是 O(1)。这就是我们对 400k+ 用户的期望:)
    • @JamesKPolk 对于相似的斐波那契数列是可解的,但我无法计算解决它所需的数学;)
    • 是的,我认为它涉及到 Wolfram Alpha 显示的 t^3 - t^2 - 2t - 3 的根。
    【解决方案3】:

    给你一个提示:

    初始化一个大小为 n 的数组来保存答案。例如,第 i 个索引将存储 a_i 的答案。将 a_0、a_1 和 a_2 初始化为给您的值(在您的情况下为 1,3 和 5)。现在从索引 3 开始迭代并使用您的公式计算 a_i。

    【讨论】:

    • 顺便说一句,您可以使用数组,但不是必须的。您只需要最后三个值。
    猜你喜欢
    • 2016-03-01
    • 2021-12-31
    • 2012-10-17
    • 2021-12-09
    • 2019-06-20
    • 1970-01-01
    • 2016-04-07
    • 2016-02-20
    • 2021-03-17
    相关资源
    最近更新 更多