Tag Archives: algorithm

Addition, Subtraction, Multiplication using bit operators

The key for addition is to think about how numbers the carry works. You want to get the sum without worrying about caring, and then sum first (XOR), and then worry about the carry bit (AND << 1). // Iterative … Continue reading

Posted in Java | Tagged | Leave a comment

Algorithms: LCM and GCD

Least Common Multiple: lcm(a,b) = abs(a*b) / gcd(a,b) = abs(a) / gcd(a,b) * abs(b) Greatest common divisor: gcd(a,b) = gcd(b, a%b); Code: int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a%b); }

Posted in Coding | Tagged , , | Leave a comment