sundeepblue
4/21/2014 - 3:52 PM

Counting trailing zeros of numbers resulted from factorial

Counting trailing zeros of numbers resulted from factorial

// solution 1, change x
int count_trailing_zeros_in_factorial(int x) {
    int count = 0;
    while(x > 0) {
        count += x / 5;
        x /= 5;
    }
    return count;
}

// solution 2, change the multipler of '5'
int count_trailing_zeros_in_factorial2(int x) {
    int count = 0;
    int f = 5;
    while(x >= f) {        // gist, >= not >
        count += x / f;
        f *= 5;
    }
    return count;
}