Given a positive integer N. Find the largest divisor of the given number that is not divisible by a perfect square greater than 1.
Input : 12 Output : 6 Explanation : Divisors of 12 are 1, 2, 3, 4, 6 and 12. Since 12 is divisible by 4 (a perfect square), it can't be required divisor. 6 is not divisible by any perfect square. Input :97 Output :97
A simple approach is to find all the divisors of the given number N by iterating upto square root of N and keep them in sorted order(Descending) in a list. Here we are inserting them in a set in descending order to keep them sorted. Also, make a list of all perfect squares upto 1010 by iterating from 1 to 105.
Now for each divisor starting from the greatest one check whether it is divisible by any perfect square in the list or not. If a divisor is not divisible by any perfect, simply return it as the answer.
Below is the implementation of the above approach.
An efficient approach is to divide n by i for every i such that (i * i) divides n.
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.
- Find smallest perfect square number A such that N + A is also a perfect square number
- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Check if a number is a perfect square having all its digits as a perfect square
- Largest number that is not a perfect square
- Minimum divisor of a number to make the number perfect cube
- Number of times the largest perfect square number can be subtracted from N
- Count numbers upto N which are both perfect square and perfect cube
- 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
- Largest factor of a given number which is a perfect square
- Largest perfect square number in an Array
- Largest N digit Octal number which is a Perfect square
- Find the Largest N digit perfect square number in Base B
- Largest Divisor for each element in an array other than 1 and the number itself
- Smallest perfect square divisible by all elements of an array
- Find the Largest divisor Subset in the Array
- Largest odd divisor Game to check which player wins
- Largest number in an array that is not a perfect cube
- Check if the sum of a subarray within a given range is a perfect square or not
- Check if the square of a number is divisible by K or not
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.