J
J
Joukai2021-10-19 15:38:18
Java
Joukai, 2021-10-19 15:38:18

How to find the remainder of division of numbers using addition and subtraction?

You need to find the remainder of doing only addition and subtraction

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rPman, 2021-10-19
@rPman

we take the divisor and add it to ourselves (i.e. multiply by 2), then add the resulting number again .. until the result becomes greater than the divisible number (we don’t remember this amount, we take the previous one)
then choose:
* from the difference of the dividend and the current sum, subtract the divisor in the loop until the result becomes less than zero (the previous one will be the remainder)
* in the process of calculating the sum of the divisor, intermediate results (an array of n elements, each y * 2 * n) subtract the previous sum from the difference of the dividend, if the result is less than zero, do not remember the result and move on to the next subtotal, until we have gone through all the saved sums. The last thing we try to subtract is the divisor itself.
this algorithm is noticeably more efficient than just stupid divisor subtraction, the second option is even more efficient but consumes log(x) memory

P
pfg21, 2021-10-19
@pfg21

damn you better.
subtract the divisor from the dividend until the remainder of the subtraction is less than the divisor

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question