P
P
Philip Polikarenkov2015-09-08 15:11:07
Python
Philip Polikarenkov, 2015-09-08 15:11:07

Why doesn't the recursive function work?

I can't figure out why the code below is not working. The idea is this: there is a limit from 1 to 11, to the number from [1, 11] we add a number not from the range, but as soon as we reach the extreme limit, we continue to add from the number on which the limit begins.
For example: the range is from 1 to 3, we add 4 to 2 and get 3. (i.e. 2+(1)=3, 3+(1)=1, 1+(1)=2, 2 +(1 ) = 3.

def ab(a,b):
    rangA = 1
    rangB = 11
    ans = a+b
    if ans <= rangB:
        return ans
    else:
        c = b - (rangB-a)
        a = 0
        ab(a,c)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2015-09-08
@Vilibb

In the recursion call, in the last line, shouldn't it be like this:
?

M
Maxim Moseychuk, 2015-09-09
@fshp

A small offtopic, but suddenly come in handy.
Here you can do without recursion. Operations in the residue ring are performed modulo the base, i.e. the result is the remainder of the division (in your case by 11). But this will give us the range [0,10] since the remainder of dividing 11 by 11 is 0. But 0 and 11 belong to the same class of residues (they have the same remainders when divided by the base), which means they are equivalent, and we can replace one with the other, thereby obtaining the range [11, 1..10 ]:

z = 11 #основание
def ab(a,b):
    res = (a + b) % z
    if res == 0:
        return z
    else:
        return res

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question