# Using piecewise-defined functions for recursive integer sequence

I have with div --> // ("integer part" of division).

I wonder how to use piecewise-defined functions for calculating n(i) and Q(i).
This does not work:

r = 1
m = 7
n(i) = piecewise([([0,0], r//m), ((0,oo), 10*(n(i-1)-Q(i-1)*m))])
Q(i) = piecewise([([0,0], 0), ((1,oo), n(i)//m)])

print n(0)
print n(1)
print n(5) edit retag close merge delete

Sort by » oldest newest most voted

r = 10

m = 2

def fonction_n(k):

if k == 0:

return r//m

return 10 * (fonction_n(k-1) -  fonction_q(k-1) * m)


def fonction_q(k):

if k == 0:

return 0

return fonction_n(k)//m


It is possible to derecursify this functions

happy new year

more

With def ....: if... it is clear. I ask myself: Is there a way with piecewise.

it is not a sagemath problem but an interesting mathematical question, right?

I write two or three equations, just to see if the answer is within pen's reach

define F(x)=10(x%n)

so n(k)=F(F(F....(F(r//m)...) with k iterations.

What do you mean by a piecewise function , when the function is defined for three variables k, r and m ?

happy new year