Recursive function that returns the remainder
up vote
0
down vote
favorite
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))
python function recursion
|
show 2 more comments
up vote
0
down vote
favorite
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))
python function recursion
are you allowed to use the%
operator? ...
– hiro protagonist
Nov 10 at 12:25
sorry,no, you may not, I will edit the question now.
– Mister Tusk
Nov 10 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 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 thatelif
part though.
– kabanus
Nov 10 at 12:30
i was trying to be sarcastic... that went the wrong way. sorry.
– hiro protagonist
Nov 10 at 12:31
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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))
python function recursion
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))
python function recursion
python function recursion
edited Nov 10 at 12:27
asked Nov 10 at 12:15
Mister Tusk
1059
1059
are you allowed to use the%
operator? ...
– hiro protagonist
Nov 10 at 12:25
sorry,no, you may not, I will edit the question now.
– Mister Tusk
Nov 10 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 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 thatelif
part though.
– kabanus
Nov 10 at 12:30
i was trying to be sarcastic... that went the wrong way. sorry.
– hiro protagonist
Nov 10 at 12:31
|
show 2 more comments
are you allowed to use the%
operator? ...
– hiro protagonist
Nov 10 at 12:25
sorry,no, you may not, I will edit the question now.
– Mister Tusk
Nov 10 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 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 thatelif
part though.
– kabanus
Nov 10 at 12:30
i was trying to be sarcastic... that went the wrong way. sorry.
– hiro protagonist
Nov 10 at 12:31
are you allowed to use the
%
operator? ...– hiro protagonist
Nov 10 at 12:25
are you allowed to use the
%
operator? ...– hiro protagonist
Nov 10 at 12:25
sorry,no, you may not, I will edit the question now.
– Mister Tusk
Nov 10 at 12:27
sorry,no, you may not, I will edit the question now.
– Mister Tusk
Nov 10 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 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 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 at 12:30
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 at 12:30
i was trying to be sarcastic... that went the wrong way. sorry.
– hiro protagonist
Nov 10 at 12:31
i was trying to be sarcastic... that went the wrong way. sorry.
– hiro protagonist
Nov 10 at 12:31
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
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).
New contributor
add a comment |
up vote
2
down vote
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 ...
That's definitely better, thanks! how would you extend this to work for negative numbers?
– Mister Tusk
Nov 10 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 stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
– quant
Nov 10 at 12:41
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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).
New contributor
add a comment |
up vote
2
down vote
accepted
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).
New contributor
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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).
New contributor
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).
New contributor
New contributor
answered Nov 10 at 12:37
Paritosh Singh
2016
2016
New contributor
New contributor
add a comment |
add a comment |
up vote
2
down vote
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 ...
That's definitely better, thanks! how would you extend this to work for negative numbers?
– Mister Tusk
Nov 10 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 stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
– quant
Nov 10 at 12:41
add a comment |
up vote
2
down vote
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 ...
That's definitely better, thanks! how would you extend this to work for negative numbers?
– Mister Tusk
Nov 10 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 stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
– quant
Nov 10 at 12:41
add a comment |
up vote
2
down vote
up vote
2
down vote
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 ...
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 ...
answered Nov 10 at 12:30
quant
1,16811025
1,16811025
That's definitely better, thanks! how would you extend this to work for negative numbers?
– Mister Tusk
Nov 10 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 stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
– quant
Nov 10 at 12:41
add a comment |
That's definitely better, thanks! how would you extend this to work for negative numbers?
– Mister Tusk
Nov 10 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 stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
– quant
Nov 10 at 12:41
That's definitely better, thanks! how would you extend this to work for negative numbers?
– Mister Tusk
Nov 10 at 12:34
That's definitely better, thanks! how would you extend this to work for negative numbers?
– Mister Tusk
Nov 10 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 stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
– quant
Nov 10 at 12:41
@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 stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
– quant
Nov 10 at 12:41
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238859%2frecursive-function-that-returns-the-remainder%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
are you allowed to use the
%
operator? ...– hiro protagonist
Nov 10 at 12:25
sorry,no, you may not, I will edit the question now.
– Mister Tusk
Nov 10 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 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 at 12:30
i was trying to be sarcastic... that went the wrong way. sorry.
– hiro protagonist
Nov 10 at 12:31