0

I am instructed to define a recursive function in Python that finds the remainder of n divided by b with the condition to not use the "/" ,"%" or "//" operator. I have defined the following function, which works fine for positive numbers. Is there a better way to do this using recursion and simple conditions.

def division(n, b, q = 1):
    """
    parameters : a et b (integers)
    returns: the remainder of a and b
    pre-requisites : q = 1
    """
    if n <= 0 or n < b:
        if n == 0:
            print("Your division has no remainder.")
        elif n in range(0,5):
            print("Your remainder is", n)
        return 0
    else:
        return division(n - b, b, q) + q

print(division(274,5))
  • are you allowed to use the [`%`](https://docs.python.org/3/library/operator.html#operator.mod) operator? ... – hiro protagonist Nov 10 '18 at 12:25
  • sorry,no, you may not, I will edit the question now. –  Nov 10 '18 at 12:27
  • @hiroprotagonist He is "instructed to define a recursive function" ... So I would not consider a one-liner solution with "%". (As always when it comes to homework questions) – quant Nov 10 '18 at 12:27
  • Are you looking for a way to extend this to negative numbers? Your function looks recursive and simple to me. Note sure about that `elif` part though. – kabanus Nov 10 '18 at 12:30
  • i was *trying* to be sarcastic... that went the wrong way. sorry. – hiro protagonist Nov 10 '18 at 12:31
  • @kabanus Yes, I am! –  Nov 10 '18 at 12:33
  • @kabanus The modulo of negative numbers is not that well defined, different programming languages calculate the modulo of negative numbers differently. So if he want to do so he needs to explain which way to use. Furhter reading https://stackoverflow.com/questions/12754680/modulo-operator-in-python and https://stackoverflow.com/questions/11720656/modulo-operation-with-negative-numbers – quant Nov 10 '18 at 12:35

2 Answers2

1

What about

def remainder(n, q):
    if(n < q):
        return n
    return remainder(n - q, q)

print(remainder(274, 5)) # will return: 4
print(remainder(275, 5)) # will return: 0
print(remainder(123, 3)) # will return: 0

much shorter ...

quant
  • 2,184
  • 2
  • 19
  • 29
  • That's definitely better, thanks! how would you extend this to work for negative numbers? –  Nov 10 '18 at 12:34
  • @MisterTusk That depends a bit, as the modulo of negative numbers is not too well defined. There are different ways of calculating them with different results, see the excellent answer of chux here https://stackoverflow.com/questions/13683563/whats-the-difference-between-mod-and-remainder/20638659#20638659 . If you tell me which way you want, I will update my answer accordingly. – quant Nov 10 '18 at 12:41
1

I believe your teacher was probably only trying to go for remainders without quotients.

def division(n, b):
    if n < b:
        return n
    return division(n - b, b)
print(division(274, 5))

However, since you brought it up, you can do it with the quotient, without having to start with a 1 for the default.

def division(n, b, q = 0):
    if n < b:
        return n, q
    return division(n - b, b, q + 1)
print(division(274, 5))

Main takeaways, you do not need to check n for range (0,5).

Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33