python - Why does recursive sequence not work correctly? -
i'm totally new python , i'm trying print solution recursive sequence:
#input sequence variables = float(raw_input("type in = ")) n0 = int(raw_input("type in n_0 = ")) n1 = int(raw_input("type in n_1 = ")) y0 = float(raw_input("type in y_0 = ")) #define function y_n (forward iteration) def yn(n): if (n==0): return y0 else: return (1/n)-a*yn(n-1) #backward iteration def yn_back(n): return (1/a)*((1/n)-yn(n-1)) if(n1>=n0): in range(n0,n1+1): print(yn(i)) else: in range(n0,n1+1): print(yn_back(i))
but if run script a=5
, n0=1
, n1=30
, y0=log(5/6)=0.182322
solutions high (from 0.08839
3.29e+18
) , values negative n. solution right n=1
. other n
, (1/n)
in definition of yn(n)
seems ignored.
can me?
thanks lot!
n
integer, 1/n
returning 0
n
greater 1:
>>> 1/1 1 >>> 1/2 0 >>> 1.0/2 0.5
to make sure you're using float division, change 1
1.0
wherever calculate 1/n
:
(1.0/n)
or convert n
float
.
Comments
Post a Comment