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!: All prime less than 16 will be the prime factors of 16!. so instead of finding all the prime factor of 16!. we just need to find the primes less than 16 that will automatically be the prime 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 firstname.lastname@example.org. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Check if a number exists having exactly N factors and K prime factors
- Maximum number of prime factors a number can have with exactly x factors
- Efficient program to print all prime factors of a given number
- Print all prime factors and their powers
- Prime factors of LCM of array elements
- Super Ugly Number (Number whose prime factors are in given set)
- Generation of n numbers with given set of factors
- Number with maximum number of prime factors
- Program to print factors of a number in pairs
- Find minimum sum of factors of number
- Maximum number of unique prime factors
- Efficient program to print the number of factors of n numbers
- Product of factors of number
- Check whether a number has exactly three distinct factors or not
- Ways to express a number as product of two different factors
- Queries to find whether a number has exactly four distinct factors or not
- Number of steps to convert to prime factors
- Prime factors of a big number
- Maximum factors formed by two numbers