I was trying to solve an old question:
Write a function that add two [integer] numbers A and B. You should not use + or any arithmetic operators.
The best solution is like this, quoted from "LintCode-A+B Problem":
For a + b in any base, we can treat the plus as two part: 1. a + b without carry; 2. the carry generated by a +b. The a+b then equals to part 1 plus part 2. If part1+part2 generates more carry, we can then repeat this procedure, until there is no carry.
I can understand this algorithm and everything seems good, so I tested it on lintcode with code pasted below.
class Solution:
"""
@param a: The first integer
@param b: The second integer
@return: The sum of a and b
"""
def aplusb(self, a, b):
while b != 0:
carry = a & b
a = a ^ b
b = carry << 1
return a
But surprisingly, it gave me Time Limit Exceeded error in test case [100, -100]. So I ran it locally and print a, b for every loop:
(-8, 8)
(-16, 16)
(-32, 32)
(-64, 64)
(-128, 128)
(-256, 256)
(-512, 512)
(-1024, 1024)
(-2048, 2048)
(-4096, 4096)
(-8192, 8192)
(-16384, 16384)
(-32768, 32768)
(-65536, 65536)
(-131072, 131072)
...
The calculation is correct, so I think this algorithm does not work for such input but when I wrote the same algorithm in C++, it just works:
class Solution {
public:
int aplusb(int a, int b) {
while (b!=0){
int carry = a & b;
a = a^b;
b = carry << 1;
}
return a;
}
};
I don't know what should be asked exactly, basically the questions are:
- Why does C++ give the correct output
0while Python doesn't? - If I use Python, how do I modify this algorithm to make it work?