Basic Math

Square

Note

Tested on: UVA152

#define SQUARE(x) ((x) * (x))

Power of 2

Check if a number is power of 2

Warning

NOT TESTED

#define isPowerOf2(x) (((x) != 0) && (((x) & ((x) - 1)) == 0))

Greatest Common Divisor (GCD)

Note

Tested on: UVA11417, UVA10976, UVA12060

inline int gcd(int a, int b)
{
    return (b == 0 ? a : gcd(b, a % b));
}

Least Common Multiple (LCM)

Warning

NOT TESTED

LCM uses gcd function presented on this section.

inline int lcm(int a, int b)
{
    // a * b / gcd
    return (a/gcd(a, b))*b;
}

Least Significant Bit (LSB)

Note

Tested on: URI1500, URI1804, UVA12086

#define LSB(x) ((x) & (-(x)))