Given an array of integers. We are required to write a program to print the number of factors of every element of the given array.
Input: 10 12 14 Output: 4 6 4 Explanation: There are 4 factors of 10 (1, 2, 5, 10) and 6 of 12 and 4 of 14. Input: 100 1000 10000 Output: 9 16 25 Explanation: There are 9 factors of 100 and 16 of 1000 and 25 of 10000.
Simple Approach: A simple approach will be to run two nested loops. One for traversing the array and other for calculating all factors of elements of array.
Time Complexity: O( n * n)
Auxiliary Space: O( 1 )
Efficient Approach: We can optimize the above approach by optimizing the number of operations required to calculate the factors of a number. We can calculate the factors of a number n in sqrt(n) operations using this approach.
Time Complexity: O( n * sqrt(n))
Auxiliary Space: O( 1 )
Best Approach: If you go through number theory, you will find an efficient way to find the number of factors. If we take a number, say in this case 30, then the prime factors of 30 will be 2, 3, 5 with count of each of these being 1 time, so total number of factors of 30 will be (1+1)*(1+1)*(1+1) = 8.
Therefore, the general formula of total number of factors of a given number will be:
Factors = (1+a1) * (1+a2) * (1+a3) * … (1+an)
where a1, a2, a3 …. an are count of distinct prime factors of n.
Let’s take another example to make things more clear. Let the number be 100 this time,
So 100 will have 2, 2, 5, 5. So the count of distinct prime factors of 100 are 2, 2. Hence number of factors will be (2+1)*(2+1) = 9.
Now, the best way to find the prime factorization will be to store the sieve of prime factors initially. Create the sieve in a way that it stores the smallest primes factor that divides itself. We can modify the Sieve of Eratostheneses to do this. Then simply for every number find the count of prime factors and multiply it to find the number of total factors .
Below is the implementation of above approach.
4 8 9 18 8
Time Complexity: O(n * log(max(number))), where n is total number of elements in the array and max(number) represents the maximum number in the array.
Auxiliary Space: O(n) for calculating the sieve.
This article is contributed by Raja Vikramaditya. 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.
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.
- Efficient program to print all prime factors of a given number
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Maximum number of prime factors a number can have with exactly x factors
- Find number of factors of N when location of its two factors whose product is N is given
- Check if a number exists having exactly N factors and K prime factors
- Program to print factors of a number in pairs
- C Program for efficiently print all prime factors of a given number
- Java Program for efficiently print all prime factors of a given number
- Write an Efficient C Program to Reverse Bits of a Number
- Efficient program to calculate e^x
- Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n!
- Number of factors of very large number N modulo M where M is any prime number
- Number of distinct prime factors of first n natural numbers
- Program to find all Factors of a Number using recursion
- Write an Efficient Method to Check if a Number is Multiple of 3
- An efficient way to check whether n-th Fibonacci number is multiple of 10
- Space efficient iterative method to Fibonacci number
- Check if a given number is Pronic | Efficient Approach
- Print all prime factors and their powers
- Print all combinations of factors (Ways to factorize)