Search code examples
pythonrecursionmemoization

Solving multiple formulas using recursion


I have to use recursion to compute the value of the following formulas:

m = 1.4 *t + 1.2*z + 0.8*l + 0.1*o

o = 1.0 *g + 1.3 *g + 1.3 *f + 0.2 *t

t = 0.9*g + 0.9 *f

l = 1.8 *t + 1.7 *g + 0.7 *o

With f= 1.4, z= 1.7, g= 1.9

I'm not sure how should I proceed into tranforming this into a recursive function.


Solution

  • from sympy import symbols
    
    f, z, g, m, o, t, l = symbols('f z g m o t l')
    
    f = 1.4
    z = 1.7
    g= 1.9
    
    t = 0.9*g + 0.9 *f
    
    o = 1.0 *g + 1.3 *g + 1.3 *f + 0.2 *t
    
    l = 1.8 *t + 1.7 *g + 0.7 *o
    
    m = 1.4 *t + 1.2*z + 0.8*l + 0.1*o
    
    print(m)
    
    # 17.53624