简单递推方程

时间:2019-04-13 19:27:53

标签: python python-3.x recursion

从C1 = 3,C2 = -2和C3 = -1开始,我需要编写一个遵循以下公式的代码:

thenextanswer = thelatestanswer + (2 * thesecondanswerback) - (3 * thethirdanswerback)

为了帮助您更容易理解,我用简单的术语将其写出:

C4 = C3 + 2*C2 - 3*C1
C5 = C4 + 2*C3 - 3*C2
C6 = C5 + 2*C4 - 3*C3
C7 = C6 + 2*C5 - 3*C4
C8 = C7 + 2*C6 - 3*C5

一直到C55

我掌握了如何完成所有递归答案的数学方法,只是在将其放入python并让python计算它时遇到了麻烦。到目前为止,这是我的代码(几乎就是猜测工作并写下基本思路):http://prntscr.com/nbigl5

关于我将如何执行此操作的任何提示/技巧?

3 个答案:

答案 0 :(得分:1)

我不认为这一定是递归的,只是看起来像一个开放形式的表达式

c = [3, -2, -1]
for i in range(3,55): # index starts at 0, 3 <= i < 55
    c.append(c[i-1] + (2*c[i-2]) - (3*c[i-3]))

如果需要递归,可以执行类似的操作

def calculate(n):
    if n == 1:
        c[n] = 3
    elif n == 2:
        c[n] = -2
    elif n == 3:
        c[n] = -1
    else:
        if n-3 in c.keys():
            z = c[n-3]
        else:
            z = calculate(n-3)
        if n-2 in c.keys():
            y = c[n-2]
        else:
            y = calculate(n-2)
        if n-1 in c.keys():
            x = c[n-1]
        else:
            x = calculate(n-1)
        c[n] = x + 2*y - 3*z
    return c[n]

c = {}
calculate(55)
print(c)

给出输出:

{1: 3, 2: -2, 3: -1, 4: -14, 5: -10, 6: -35, 7: -13, 8: -53, 9: 26, 10: -41, 11: 170, 12: 10, 13: 473, 14: -17, 15: 899, 16: -554, 17: 1295, 18: -2510, 19: 1742, 20: -7163, 21: 3851, 22: -15701, 23: 13490, 24: -29465, 25: 44618, 26: -54782, 27: 122849, 28: -120569, 29: 289475, 30: -320210, 31: 620447, 32: -888398, 33: 1313126, 34: -2325011, 35: 2966435, 36: -5622965, 37: 7284938, 38: -12860297, 39: 18578474, 40: -28996934, 41: 46740905, 42: -66988385, 43: 113484227, 44: -160715258, 45: 267218351, 46: -394664846, 47: 621917630, 48: -969067115, 49: 1458762683, 50: -2345124437, 51: 3479602274, 52: -5586934649, 53: 8407643210, 54: -13205032910, 55: 20371057457}

编辑: 进行真正递归的函数(基于下面的iElden回答,但是运行时我没有得到正确的结果,但是我进行了一些更改,它比我想出的要有效得多)

def calc(c3, c2, c1, n, maxn):
    result = c3 + 2*c2 - 3*c1
    print("n: "+str(n)+" r: "+str(result))
    if n == maxn:
        return result
    else:
        return calc(result, c3, c2, n + 1, maxn)
calc(-1, -2, 3, 4, 20)

我的真实递归需要花费很长的时间才能计算出来,但是得到正确的答案,并且更符合递归的精神

def recursive(n):
    if n == 1:
        r = 3
    elif n == 2:
        r = -2
    elif n == 3:
        r = -1
    else:
        z = recursive(n-3)
        y = recursive(n-2)
        x = recursive(n-1)
        r = x + 2*y - 3*z
    print("n: "+str(n)+" r: "+str(r))
    return r
recursive(20)

通过C20输出

n: 4 r: -14
n: 5 r: -10
n: 6 r: -35
n: 7 r: -13
n: 8 r: -53
n: 9 r: 26
n: 10 r: -41
n: 11 r: 170
n: 12 r: 10
n: 13 r: 473
n: 14 r: -17
n: 15 r: 899
n: 16 r: -554
n: 17 r: 1295
n: 18 r: -2510
n: 19 r: 1742
n: 20 r: -7163

答案 1 :(得分:1)

要创建递归函数时,要做的第一件事就是定义停止条件。
在这里,停止条件是当您的结果为C55时,我将此变量命名为loop_nb

然后,您必须查看每个步骤之间要传输的内容,在这里,您要传输结果和前两个参数。

在代码中它给出了:

def calc(c3, c2, c1, loop_nb):
    result = c3 + 2*c2 - 3*c1
    if loop_nb == 55:
        return result
    else:
        return calc(result, c3, c2, loop_nb + 1)


calc(-1, -2, 3, 4)

我希望它对您的家庭作业有用!

答案 2 :(得分:0)

要回答并总结现有答案,我们可以通过以下方式使用简单且合理快速的la @iElden(+1)和@MatthewK(+1)递归:

def simply_recursive(c1, c2, c3, n=55):
    c4 = c3 + 2 * c2 - 3 * c1

    if n == 4:
        return c4

    return simply_recursive(c2, c3, c4, n - 1)

print(simply_recursive(3, -2, -1))  # 20371057457

或者我们可以采用@MatthewK进行多次递归的慢速解决方案,并使用缓存来加速它以优化冗余调用:

from functools import lru_cache

@lru_cache()
def massively_recursive(n):
    if n == 1:
        return 3

    if n == 2:
        return -2

    if n == 3:
        return -1

    c1 = massively_recursive(n - 3)
    c2 = massively_recursive(n - 2)
    c3 = massively_recursive(n - 1)

    return c3 + 2 * c2 - 3 * c1

print(massively_recursive(55))  # 20371057457

两者的性能大致相同:

> time python3 test1.py
20371057457
0.029u 0.006s 0:00.04 50.0% 0+0k 0+0io 0pf+0w
> time python3 test2.py
20371057457
0.027u 0.006s 0:00.04 50.0% 0+0k 0+0io 0pf+0w
>