
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find four factors of N with maximum product and sum equal to N - Set-2 in Python
Suppose we have a number N; we have to find factors of N and return only the product of four factors of N such that −
The sum of the four factors is same as N.
The product of the four factors is maximum.
All four factors can be equal to each other to maximize the product.
So, if the input is like N = 60, then the output will be 50625 as all the factors are → 1 2 3 4 5 6 10 12 15 20 30 60 and their product is 50625, as we 15 has been selected four times to make product largest.
To solve this, we will follow these steps −
my_map := a new map
v := a new list, v1 := a new list
for i in range 1 to the ceiling of square root of (n) + 1, do
if n mod i is same as 0, then
insert i at the end of v
if i is not same as integer part of (n / i) and i is not same as 1, then
insert integer part of (n / i) at the end of v
s := size of v
maximum := -1
map1 := an array of size (n + 5) fill this with 0
for i in range 0 to s, do
for j in range i to s, do
if v[i] + v[j] < n , then
insert v[i] + v[j] at the end of v1
map1[v[i] + v[j]] := [v[i], v[j]]
my_map[v[i] + v[j]] := 1
s := size of v1
for i in range 0 to s, do
element := n - (v1[i])
if element in my_map, then
a := map1[v1[i], 0]
b := map1[v1[i], 1]
c := map1[n - v1[i], 0]
d := map1[n - v1[i], 1]
maximum := maximum of a * b * c * d, maximum
if maximum is same as -1, then
display "Not Possible"
otherwise,
display maximum
Example
Let us see the following implementation to get better understanding −
from math import sqrt, ceil, floor def get_product(n): my_map = dict() v = [] v1 = [] for i in range(1,ceil(sqrt(n)) + 1): if (n % i == 0): v.append(i) if (i != (n // i) and i != 1): v.append(n // i) s = len(v) maximum = -1 map1 = [0]*(n + 5) for i in range(s): for j in range(i, s): if (v[i] + v[j] < n): v1.append(v[i] + v[j]) map1[v[i] + v[j]] =[v[i], v[j]] my_map[v[i] + v[j]] = 1 s = len(v1) for i in range(s): element = n - (v1[i]) if (element in my_map): a = map1[v1[i]][0] b = map1[v1[i]][1] c = map1[n - v1[i]][0] d = map1[n - v1[i]][1] maximum = max(a * b * c * d, maximum) if (maximum == -1): print("Not Possible") else : print("Maximum product", maximum) n = 60 get_product(n)
Input
60
Output
Maximum product 50625
- Related Articles
- Find four factors of N with maximum product and sum equal to N - Set-2 in Python Program
- Find four factors of N with maximum product and sum equal to N - Set-2 in C++
- C++ find four factors of N with maximum product and sum equal to N .
- Find four factors of N with maximum product and sum equal to N in C++
- Find maximum product of digits among numbers less than or equal to N in C++
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- Find sum of Series with n-th term as n^2 - (n-1)^2 in C++
- Java Program to Find sum of Series with n-th term as n^2 – (n-1)^2
- Find N integers with given difference between product and sum in C++
- Maximum Primes whose sum is equal to given N in C++
- Maximum GCD of N integers with given product in C++
- Find two numbers with sum and product both same as N in C++
- C++ program to find two numbers with sum and product both same as N
- Finding n subarrays with equal sum in JavaScript
- C/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2
