Given a perfect square natural number N. The task is to find all the factors of N.
Input: N = 100
Output: 1 2 4 5 10 20 25 50 100
Input: N = 900
Output: 1 2 4 3 6 12 9 18 36 5 10 20 15 30 60 45 90 180 25 50 100 75 150 300 225 450 900
- Find the square root of N in temp.
- Find all the prime factors of temp in O(sqrt(temp)) using the approach discussed in this article.
- Initialise an array factor with element 1 in it.
- Store all the prime factors of temp obtained in above step twice in an array factor.
- Initialise a matrix M such that for every element in factor starting from index 1:
- If factor[i] is equals to factor[i-1], then store factor[i]*factor[i-1] in matrix M in row i – 1 .
- Else factor[i] is not equals to factor[i-1], then store factor[i]*factor[i-1] in matrix M in row i.
- Initialise two arrays arr1 and arr2 with the element 1 in both the array.
- Iterate over every row of matrix M such that the product of every element in arr1 with every element of current row must be stored in arr2.
- After above step, copy every element of arr2 in arr1.
- Repeat above two steps, till all the element of matrixM is traverse.
- The array arr2 contains all the factors of number N.
Below is the implementation of the above approach:
1 2 4 3 6 12 9 18 36 5 10 20 15 30 60 45 90 180 25 50 100 75 150 300 225 450 900
Time Complexity: O(sqrt(sqrt(N)))
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find smallest perfect square number A such that N + A is also a perfect square number
- Find the Next perfect square greater than a given number
- Find minimum number to be divided to make a number a perfect square
- Find the Largest N digit perfect square number in Base B
- Array range queries to find the number of perfect square elements with updates
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Check if a number is perfect square without finding square root
- 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
- Find number of factors of N when location of its two factors whose product is N is given
- Square of large number represented as String
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Largest number that is not a perfect square
- Check if given number is perfect square
- Number of times the largest perfect square number can be subtracted from N
- Previous perfect square and cube number smaller than number N
- Largest factor of a given number which is a perfect square
- Largest perfect square number in an Array
- Find pairs of elements from two different arrays whose product is a perfect square
- Check if a given number is a Perfect square using Binary Search
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.