Given a positive integer n, find the no of factors in n! where n <= 105.
Input : n = 3 Output : 4 Factors of 3! are 1, 2, 3, 6 Input : n = 4 Output : 8 Factors of 4! are 1, 2, 3, 4, 6, 8, 12, 24 Input : n = 16 Output : 5376
Note that the brute force approach won’t even work here because we can’t find n! for such large n. We would need a more realistic approach to solve this problem.
The idea is based on Legendre’s formula.
Any positive integer can be expressed as product of power of its prime factors. Suppose a number n = p1a1 x p2a2 x p3a3, …., pkak where p1, p2, p3, …., pk are distinct primes and a1, a2, a3,………….., ak are their respective exponents.
Then the no of divisors of n = (a1+1) x (a2+1) x (a3+1)…x (ak+1)
Thus, no. of factors of n! can now be easily computed by first finding the prime factors till n and then calculating their respective exponents.
The main steps of our algorithm are:
- Iterate from p = 1 to p = n and at each iteration check if p is prime.
- If p is prime then it means it is prime factor of n! so we find exponent of p in n! which is
- After finding the respective exponents of all prime factors let’s say they are a1, a2 , a3, …., ak then the factors of n! = (a1+1) x (a2+1) x (a3+1)……………(ak+1)
Here is an illustration on how the algorithm works for finding factors of 16!: Prime factors of 16! are: 2,3,5,7,11,13 Now to the exponent of 2 in 16! = ⌊16/2⌋+ ⌊16/4⌋+ ⌊16/8⌋ + ⌊16/16⌋ = 8 + 4 + 2 + 1 = 15 Similarly, exponent of 3 in 16! = ⌊16/3⌋ + ⌊16/9⌋ = 6 exponent of 5 in 16! = 3 exponent of 7 in 16! = 2 exponent of 11 in 16! = 1 exponent of 13 in 16! = 1 So, the no of factors of 16! = (15+1) * (6+1) * (3+1) *(2+1)* (1+1) * (1+1) = 5376
Below is the implementation of above idea:
Count of factors of 16! is 5376
Note : If the task is to count factors for multiple input values, then we can precompute all prime numbers upto the maximum limit 105.
This article is contributed by Madhur Modi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Find number of factors of N when location of its two factors whose product is N is given
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Sum of all the factors of a number
- Sum of all even factors of numbers in the range [l, r]
- Find sum of even factors of a number
- Find sum of odd factors of a number
- Sum of all odd factors of numbers in the range [l, r]
- Prime factors of a big number
- Product of factors of number
- Generation of n numbers with given set of factors
- Smallest integer which has n factors or more
- Expressing a number as sum of consecutive | Set 2 (Using odd factors)
- Common prime factors of two numbers
- Number of elements with even factors in the given range
- Maximize the product of four factors of a Number