Find four factors of N with maximum product and sum equal to N | Set | Python3 program to find four factors of N with maximum product and sum equal to N ; Function to find factors and to prthose four factors ; push all the factors in the container ; number of factors ; Initial maximum ; hash - array to mark the pairs ; form all the pair sums ; if the pair sum is less than n ; push in another container ; mark the sum with the elements formed ; mark in the map that v [ i ] + v [ j ] is present ; new size of all the pair sums ; iterate for all pair sum ; the required part ; if the required part is also present in pair sum ; find the elements with which the first pair is formed ; find the elements with which the second pair is formed ; check for previous maximum ; Driver code
from math import sqrt , ceil , floor NEW_LINE def findfactors ( n ) : NEW_LINE INDENT mpp = dict ( ) NEW_LINE v = [ ] NEW_LINE v1 = [ ] NEW_LINE for i in range ( 1 , ceil ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE if ( i != ( n // i ) and i != 1 ) : NEW_LINE INDENT v . append ( n // i ) NEW_LINE DEDENT DEDENT DEDENT s = len ( v ) NEW_LINE maxi = - 1 NEW_LINE mp1 = [ 0 ] * ( n + 5 ) NEW_LINE for i in range ( s ) : NEW_LINE INDENT for j in range ( i , s ) : NEW_LINE INDENT if ( v [ i ] + v [ j ] < n ) : NEW_LINE INDENT v1 . append ( v [ i ] + v [ j ] ) NEW_LINE mp1 [ v [ i ] + v [ j ] ] = [ v [ i ] , v [ j ] ] NEW_LINE mpp [ v [ i ] + v [ j ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT s = len ( v1 ) NEW_LINE for i in range ( s ) : NEW_LINE INDENT el = n - ( v1 [ i ] ) NEW_LINE if ( el in mpp ) : NEW_LINE INDENT a = mp1 [ v1 [ i ] ] [ 0 ] NEW_LINE b = mp1 [ v1 [ i ] ] [ 1 ] NEW_LINE c = mp1 [ n - v1 [ i ] ] [ 0 ] NEW_LINE d = mp1 [ n - v1 [ i ] ] [ 1 ] NEW_LINE maxi = max ( a * b * c * d , maxi ) NEW_LINE DEDENT DEDENT if ( maxi == - 1 ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ maximum ▁ product ▁ is ▁ " , maxi ) NEW_LINE DEDENT DEDENT n = 50 NEW_LINE findfactors ( n ) NEW_LINE