8
1
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.
I'm probably not going to accept any answer, just because there were too many loopholes in this question for it to be meaningful. – Joe Z. – 2013-09-15T16:55:12.203
1eBusiness answered the challenge well, imho. A lookup table solves the challenge without any additions. Yes, it's a bit humorous, but what the heck? I also like Johannes Kuhn's approach. You moved the goalposts to disqualify his entry. That, to me, was unfair. – DavidC – 2013-09-15T17:25:12.230
I agree that he answered the challenge well, and I upvoted his answer for that. But it feels wrong to accept an answer just because the goalposts were incorrectly placed in the first place. – Joe Z. – 2013-09-15T19:29:45.990
You managed it to disqualify 2 of my answers. Ok, the first one used the division function (which was not an operator at that time), so better leave that deleted. – Johannes Kuhn – 2013-09-17T07:29:14.500