Given an integer N, the task is to find the number of factors of N which are perfect square.
Input: N = 100
There are four factors of
100 (1, 4, 25, 100) that are perfect square.
Input: N = 900
There are eight factors of 900 (1, 4, 9, 25, 36, 100, 225, 900) that are perfect square.
Naive Approach: The simplest approach to solve this problem is to find all possible factors of the given number N and for each factor, check if the factor is a perfect square or not. For every factor found to be so, increase count. Print the final count.
Time Complexity: O(N)
Auxiliary Space: O(1)
The following observations need to be made to optimize the above approach:
The number of factors for a number is given by:
Factors of N = (1 + a1)*(1 + a2)*(1 + a3)*..*(1 + an)
where a1, a2, a3, .., an are the count of distinct prime factors of N.
In a perfect square, the count of distinct prime factors must be divisible by 2. Therefore, the count of factors that are a perfect square is given by:
Factors of N that are perfect square = (1 + a1/2)*(1 + a2/2)*…*(1 + an/2) where a1, a2, a3, .., an are the count of distinct prime factors of N.
The prime factors of N = 100 are 2, 2, 5, 5.
Therefore, the number of factors that are perfect square are (1 + 2/2) * (1 + 2/2) = 4.
The factors are 1, 4, 25, 100.
Therefore, find the count of prime factors and apply the above formula to find the count of factors that are a perfect square.
Below is the implementation of the above approach:
Time Complexity: O(log(N))
Space Complexity: O(1)
- Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))
- Find smallest perfect square number A such that N + A is also a perfect square number
- Check if a number is perfect square without finding square root
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Perfect Cube factors of a Number
- Largest number that is not a perfect square
- Check if given number is perfect square
- Find minimum number to be divided to make a number a perfect square
- Previous perfect square and cube number smaller than number N
- Number of times the largest perfect square number can be subtracted from N
- Largest perfect square number in an Array
- Largest factor of a given number which is a perfect square
- Find the Next perfect square greater than a given number
- Check whether the number can be made perfect square after adding K
- Check whether the number can be made perfect square after adding 1
- Check if a given number is a Perfect square using Binary Search
- Largest Divisor of a Number not divisible by a perfect square
- Least number to be added to or subtracted from N to make it a Perfect Square
- Largest N digit Octal number which is a Perfect square
- Minimum digits to remove to make a number Perfect Square
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.