Given x, y, How to find if there is x! divided by y or not?

computing x! can be very expensive and can often lead to overflow. Is there any way to find out if there is x! divided by y or not without calculating x !?

  • For y <x, its trivial,
  • But for y> x, for example. x = 5 and y = 60; I'm struggling to find a way without calculating x!
+4
source share
3 answers

If x and y are really large, so it is not viable to iterate over all numbers from 1 to x, you can instead simply decompose y and calculate for each simple factor whether its maximum power in y will also divide x !.

.

:

// computes maximum q so that p^q divides n!
bool max_power_of_p_in_fac(int p, int n) { 
    int mu = 0;
    while (n/p > 0) {
        mu += n/p;
        n /= p;
    }
    return mu;
}
// checks whether y divides x!
bool y_divides_x_fac(int y, int x) {
    for each prime factor p^q of y:
        if (max_power_of_p_in_fac(p, x) < q)
            return false;
    return true;
}

x < y O ( y + log x * # y).

, y O (log y) . , - O (y ^ (1/4) + log x * log y)

, :

enter image description here

+6

x! y. x!, 2 x . y x!, .

+7

i 1 x y /= gcd(y, i). - y == 1.

+2

Source: https://habr.com/ru/post/1538465/


All Articles