Given a number . The task is to find the largest factor of that number which is a perfect square.
Input : N = 420 Output : 4 Input : N = 100 Output : 100
A Simple Solution is to traverse all of the numbers in decreasing order from the given number down till 1 and if any of these numbers is a factor of the given number and is also a perfect square, print that number.
Time Complexity: O(N)
Better Solution : A better solution is to use prime factorization of the given number.
- First find all the prime factors of that number till sqrt(num).
- Take a variable, answer and initialize it to 1. It represents the largest square number which is also a factor of that number.
- Now, Check If any prime number occurs even number of times in the prime factorization of the given number, if yes then multiply the answer with that prime factor the number of times it occurs.
- Else, if it occurs odd number of times then multiply the answer with prime number (K – 1) times, K is the frequency of that prime factor in the prime factorization.
Below is the implementation of the above approach:
Time Complexity: O( sqrt(N) )
- Largest number that is not a perfect square
- Largest perfect square number in an Array
- Largest N digit Octal number which is a Perfect square
- Largest Divisor of a Number not divisible by a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Check if a number is perfect square without finding square root
- Sum of largest prime factor of each number less than equal to n
- Find largest prime factor of a number
- Check if given number is perfect square
- Find minimum number to be divided to make a number a perfect square
- Find the Next perfect square greater than a given number
- Check whether the number can be made perfect square after adding 1
- Least number to be added to or subtracted from N to make it a Perfect Square
- Check whether the number can be made perfect square after adding K
- Count numbers upto N which are both perfect square and perfect cube
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.