Largest Divisor of a Number not divisible by a perfect square | Python3 Program to find the largest divisor not divisible by any perfect square greater than 1 ; Function to find the largest divisor not divisible by any perfect square greater than 1 ; set to store divisors in descending order ; If the number is divisible by i , then insert it ; Vector to store perfect squares ; Check for each divisor , if it is not divisible by any perfect square , simply return it as the answer . ; Driver Code
MAX = 10 ** 5 NEW_LINE def findLargestDivisor ( n ) : NEW_LINE INDENT m = n NEW_LINE s = set ( ) NEW_LINE s . add ( 1 ) NEW_LINE s . add ( n ) NEW_LINE for i in range ( 2 , int ( n ** ( 0.5 ) ) + 1 ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT s . add ( n // i ) NEW_LINE s . add ( i ) NEW_LINE while m % i == 0 : NEW_LINE INDENT m //= i NEW_LINE DEDENT DEDENT DEDENT if m > 1 : NEW_LINE INDENT s . add ( m ) NEW_LINE DEDENT vec = [ i ** 2 for i in range ( 2 , MAX + 1 ) ] NEW_LINE for d in sorted ( s , reverse = True ) : NEW_LINE INDENT divi , j = 0 , 0 NEW_LINE while j < len ( vec ) and vec [ j ] <= d : NEW_LINE INDENT if d % vec [ j ] == 0 : NEW_LINE INDENT divi = 1 NEW_LINE break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if not divi : NEW_LINE INDENT return d NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE print ( findLargestDivisor ( n ) ) NEW_LINE n = 97 NEW_LINE print ( findLargestDivisor ( n ) ) NEW_LINE DEDENT