The expression algorithm n! as a product of the degrees of prime

What could be the simplest and most time-efficient logic for expressing n! how is the product of degrees simple?

What interests me most is to find the degrees of simple so that I can know the number of factors. How n! can be expressed as p1 ^ e1 * p2 ^ e2 * ... * pk ^ ek, where each p is a prime number, then the number of factors n is (e1 + 1) (e2 + 1) ... * (ek + 1 )

+3
source share
1 answer

The most effective way to find the simple factorization n!that I know of is by counting how often each prime number acts as a factor in n!. Obviously, nothing simple is greater than n, so let it p <= nbe simple.

1, ..., n

q1 = floor(n/p)

p.

q2 = floor(q1/p) = floor(n/p²)

.

q3 = floor(q2/p) = floor(n/p³)

. , p n!

q1 + q2 + q3 + ...

(An a = p^k*b, b, p, k k, q1, ..., qk.) :

unsigned long long factorial_exponent(unsigned long long n, unsigned long long p) {
    unsigned long long exponent = 0;
    do {
        n /= p;
        exponent += n;
    }while(n);
    return exponent;
}

floor(log n/log p) + 1 , , , n,

log n * ∑ (1/log p + 1) ≈ 2n/log n
       p≤n

. (: ≤ n > √n, p > √n, , q2 = 0, : n/p, , .)

, n, , , O (n) O (n/log log n) ( , , log log n ), Eratosthenes, , n in - - O (n * log log n) O (n) .

( n - ).

, , k ≤ n, , . c*n^1.5/log n - c, log n , n^1.5. ( ) , O (n * log log n), , , , k p k/p " " q of k/p, k/(p*q) .. , - , .

, k ≈ log log k, O (n * log log n). , , ​​ , .

+13

All Articles