Smallest divisor D of N such that gcd ( D , M ) is greater than 1 | Python3 implementation of the above approach ; Function to find the minimum divisor ; Iterate for all factors of N ; Check for gcd > 1 ; Check for gcd > 1 ; If gcd is m itself ; Drivers code
import math NEW_LINE def findMinimum ( n , m ) : NEW_LINE INDENT mini , i = m , 1 NEW_LINE while i * i <= n : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT sec = n // i NEW_LINE if math . gcd ( m , i ) > 1 : NEW_LINE INDENT return i NEW_LINE DEDENT elif math . gcd ( sec , m ) > 1 : NEW_LINE INDENT mini = min ( sec , mini ) NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT if mini == m : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return mini NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , m = 8 , 10 NEW_LINE print ( findMinimum ( n , m ) ) NEW_LINE DEDENT