3 ms·
> My code also doesn't handle negative integers correctly, but I'm pretty sure there is a way to do it. It's pretty easy to show by induction that you can't ha
by thaumasiotes 12y ago
> My code also doesn't handle negative integers correctly, but I'm pretty sure there is a way to do it.
It's pretty easy to show by induction that you can't handle negative numbers in the general case. Consider: if memory locations 0 and 1 are both negative, so is their sum. But the only operations available to you are setting a value to 0, and incrementing it. You can't produce a negative number that way.
- btilly 12y agoThis is true. But if you are clever, you can handle the case where you have 2 arbitrary integers whose sum is non-negative! The trick is to zero out a counter, then increment it until it is one of the two starting values. Then start a second counter and increment it together with the other starting value until the second counter reaches the first and the other contains the sum of the two. Finally start a counter at location 2 and increment it until it reaches the value of the register that has the sum.