There is a question on the site that asks to implement division without using division.
In my case, I am asking you to do the same, but only using addition.
What this means is basically: addition is the only operator or function allowed that operates on numbers and returns other numbers (i.e. no subtraction, multiplication, exponentiation, bitwise inversion, etc.). Stuff like if statements, assignment and comparison operators, and for loops are still allowed, provided that within those, you still only use addition.
Your task is to build a function divide(a, b)
that takes two positive integers a
and b
and returns the result of a
being divided by b
and rounded toward zero, but using addition and no other arithmetical operators, and no other data constructs besides numbers.
The code that wins will be the one that requires the fewest addition operations to be performed over the set of inputs where a
varies from 1
to 200
and b
varies from 1
to a
.
To keep track of this, you can build an alternate version of your code that replaces every instance of a + b
with add(a, b)
and program add
to increment a global add_used
variable as well as returning the sum of the two numbers.