The greatest common divisor, least common multiple, all divisors

The greatest common divisor (removed divided)

int gcd(int x, int y)
{   
    int z = y;
    while(x%y!=0)
    {
        z = x%y;
        x = y;
        y = z;  
    }
    return z;
}

The least common multiple

int lcm(int x, int y) {
    return x/gcd(x,y)*y;
}

All divisors (excluding 1 and itself)

void factor(int n, vector<int> & arr)
{
    for(int i = 2; i <= sqrt(n); i++)
    {
        if(n % i == 0)
        {
            arr.push_back(i);
            if(n / i != i)
                arr.push_back(n / i);
        }
    }
}

Guess you like

Origin www.cnblogs.com/narjaja/p/11089125.html