0

how can I calculate the rest of a division using only add and subtraction?

1st Oct 2016, 2:36 PM
john
5 ответов
+ 7
Add or substract (depending on the sign) the denominator from the numerator till you have a number between 0 (included) and the denominator (not included).
1st Oct 2016, 3:01 PM
Zen
Zen - avatar
+ 2
You can define a function like this: def divide(n,d): rem = 0 quotient = 0 while True: if n < d: rem = n break else: n -= d quotient += 1 return quotient, rem Full code available here: https://code.sololearn.com/cLPbxIoU77mN
1st Oct 2016, 3:52 PM
Rohit Kumar
+ 1
Example: 14 % 5: 14 - 5 = 9 9 isn't between 0 and 5, so we loop. 9 - 5 = 4 4 is between 0 and 5 and is the rest we are looking for.
1st Oct 2016, 3:47 PM
Zen
Zen - avatar
0
example please...
1st Oct 2016, 3:05 PM
john
0
thanks
1st Oct 2016, 3:57 PM
john