Find largest factor of N such that N / F is less than K | Function to find the largest factor of N which is less than or equal to K ; Initialise the variable to store the largest factor of N <= K ; Loop to find all factors of N ; Check if j is a factor of N or not ; Check if j <= K If yes , then store the larger value between ans and j in ans ; Check if N / j <= K If yes , then store the larger value between ans and j in ans ; Since max value is always stored in ans , the maximum value divisible by N less than or equal to K will be returned . ; Driver Code ; Given N and K ; Function call
def solve ( n , k ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( j * j > n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT if ( n % j == 0 ) : NEW_LINE INDENT if ( j <= k ) : NEW_LINE INDENT ans = max ( ans , j ) ; NEW_LINE DEDENT if ( n // j <= k ) : NEW_LINE INDENT ans = max ( ans , n // j ) ; NEW_LINE DEDENT DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 ; K = 7 ; NEW_LINE print ( ( N // solve ( N , K ) ) ) ; NEW_LINE DEDENT