Find four factors of N with maximum product and sum equal to N

Given an integer N. The task is to find all factors of N print the product of four factors of N such that:

  • Sum of the four factors is equal to N.
  • Product of the four factors is maximum.

If it is not possible to find 4 such factors then print “Not possible”.

Note: All the four factors can be equal to each other to maximize the product.

Examples:

Input : 24
Output : Product -> 1296
All factors are -> 1 2 3 4 6 8 12 24 
Choose the factor 6 four times,
Therefore, 6+6+6+6 = 24 and product is maximum.

Input : 100
Output : Product -> 390625
All the factors are -> 1 2 4 5 10 10 20 25 50 100 
Choose the factor 25 four times.


In the second example, the product will be maximum when the four factors will be equal to 25. The sum of four factors is equal to ‘N’. Though we can choose the same factor four times to maximize the product.

Below is the step by step algorithm to solve this problem:

  1. First find the factors of a number ‘N’ by traversing from 1 to square root of ‘N’ and check if ‘i’ and ‘n/i’ divides N and store them in a vector.
  2. Sort the vector and print every element.
  3. Find three numbers to maximize the product with the fourth number, using three loops.
  4. Replace the next maximum product with the previous product.
  5. Print the product when you’ll find the four factors.

Below is the implementaion of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find four factors of N
// with maximum product and sum equal to N
#include <bits/stdc++.h>
  
using namespace std;
  
// Function to find factors
// and to print those four factors
void findfactors(int n)
{
    vector<int> vec;
  
    // inserting all the factors in a vector s
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            vec.push_back(i);
            vec.push_back(n / i);
        }
    }
  
    // sort the vector
    sort(vec.begin(), vec.end());
  
    // print all the factors
    cout << "All the factors are -> ";
    for (int i = 0; i < vec.size(); i++)
        cout << vec[i] << " ";
    cout << endl;
  
    // Any elements is divisible by 1
    int maxProduct = 1;
    bool flag = 1;
  
    // using three loop we'll find
    // the three maximum factors
    for (int i = 0; i < vec.size(); i++) {
        for (int j = i; j < vec.size(); j++) {
            for (int k = j; k < vec.size(); k++) {
                // storing the fourth factor in y
                int y = n - vec[i] - vec[j] - vec[k];
  
                // if the fouth factor become negative
                // then break
                if (y <= 0)
                    break;
  
                // we will replace more optimum number 
                // than the previous one
                if (n % y == 0) {
                    flag = 0;
                    maxProduct = max(vec[i] * vec[j] * vec[k] * y, 
                                                    maxProduct);
                }
            }
        }
    }
  
    // print the product if the numbers exist
    if (flag == 0)
        cout << "Product is -> " << maxProduct << endl;
  
    else
        cout << "Not possible" << endl;
}
  
// Driver code
int main()
{
    int n;
    n = 50;
  
    findfactors(n);
      
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.Collections;
import java.util.Vector;
  
// Java program to find four factors of N 
// with maximum product and sum equal to N 
class GFG 
{
  
    // Function to find factors 
    // and to print those four factors 
    static void findfactors(int n) 
    {
        Vector<Integer> vec = new Vector<Integer>();
  
        // inserting all the factors in a vector s 
        for (int i = 1; i * i <= n; i++) 
        {
            if (n % i == 0
            {
                vec.add(i);
                vec.add(n / i);
            }
        }
  
        // sort the vector 
        Collections.sort(vec);
  
        // print all the factors 
        System.out.println("All the factors are -> ");
        for (int i = 0; i < vec.size(); i++) 
        {
            System.out.print(vec.get(i) + " ");
        }
        System.out.println();
  
        // Any elements is divisible by 1 
        int maxProduct = 1;
        boolean flag = true;
  
        // using three loop we'll find 
        // the three maximum factors 
        for (int i = 0; i < vec.size(); i++) 
        {
            for (int j = i; j < vec.size(); j++) 
            {
                for (int k = j; k < vec.size(); k++) 
                {
                    // storing the fourth factor in y 
                    int y = n - vec.get(i) - vec.get(j) - 
                                    vec.get(k);
  
                    // if the fouth factor become negative 
                    // then break 
                    if (y <= 0)
                    {
                        break;
                    }
  
                    // we will replace more optimum number 
                    // than the previous one 
                    if (n % y == 0
                    {
                        flag = false;
                        maxProduct = Math.max(vec.get(i) * vec.get(j) * 
                                            vec.get(k) * y, maxProduct);
                    }
                }
            }
        }
  
        // print the product if the numbers exist 
        if (flag == false
        {
            System.out.println("Product is -> " + maxProduct);
        }
        else 
        {
            System.out.println("Not possible");
        }
    }
  
    // Driver code 
    public static void main(String[] args) 
    {
        int n;
        n = 50;
        findfactors(n);
    }
  
// This code is contributed by PrinciRaj1992

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 to find four factors 
# of N with maximum product and
# sum equal to N
  
# import every thing from
# math library
from math import *
  
# Function to find factors 
# and to print those four factors 
def findfactors(n) :
    vec = []
  
    # inserting all the factors
    # in a list vec
    for i in range(1, int(sqrt(n)) + 1) :
        if n % i == 0 :
            vec.append(i)
            vec.append(n // i)
  
    # sort the list
    vec.sort()
      
    print("All the factors are -> "
                           end = "")
      
    # print all the factors 
    for i in range(len(vec)) :
        print(vec[i], end = " ")
    print()
  
    # Any elements is divisible by 1 
    maxProduct = 1
    flag = 1
  
    # using three loop we'll find 
    # the three maximum factors 
    for i in range(0, len(vec)) :
        for j in range(i, len(vec)) :
            for k in range(j, len(vec)) :
  
                # storing the fourth factor in y 
                y = n - vec[i] - vec[j] - vec[k]
  
                # if the fouth factor become
                # negative then break 
                if y <= 0 :
                    break
  
                # we will replace more optimum 
                # number than the previous one 
                if n % y == 0 :
                    flag = 0
                    maxProduct = max(vec[i] * vec[j] * 
                                     vec[k] * y , maxProduct)
  
    # print the product if the numbers exist
    if flag == 0 :
        print("Product is - >", maxProduct)
    else :
        print("Not possible")
  
# Driver Code
if __name__ == "__main__" :
    n = 50
  
    # function calling
    findfactors(n)
      
# This code is contributed by ANKITRAI1

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program to find four factors 
// of N with maximum product and 
// sum equal to N
  
// Function to find factors
// and to print those four factors
function findfactors($n)
{
    $vec = array();
      
    // inserting all the 
    // factors in a vector s
    for ($i = 1; $i * $i <= $n; $i++)
    {
        if ($n % $i == 0) 
        {
            array_push($vec, $i);
            array_push($vec, ($n / $i));
        }
    }
  
    // sort the vector
    sort($vec);
  
    // print all the factors
    echo "All the factors are -> ";
    for ($i = 0; $i < sizeof($vec); $i++)
        echo $vec[$i] . " ";
    echo "\n";
  
    // Any elements is divisible by 1
    $maxProduct = 1;
    $flag = 1;
  
    // using three loop we'll find
    // the three maximum factors
    for ($i = 0; $i < sizeof($vec); $i++) 
    {
        for ($j = $i
             $j < sizeof($vec); $j++) 
        {
            for ($k = $j;
                 $k < sizeof($vec); $k++) 
            {
                // storing the fourth factor in y
                $y = $n - $vec[$i] - 
                          $vec[$j] - $vec[$k];
  
                // if the fouth factor become 
                // negative then break
                if ($y <= 0)
                    break;
  
                // we will replace more optimum 
                // number than the previous one
                if ($n % $y == 0)
                {
                    $flag = 0;
                    $maxProduct = max($vec[$i] * $vec[$j] * 
                                      $vec[$k] * $y, $maxProduct);
                }
            }
        }
    }
  
    // print the product if 
    // the numbers exist
    if ($flag == 0)
        echo "Product is -> "
             $maxProduct ."\n";
  
    else
        echo "Not possible" ."\n";
}
  
// Driver code
$n = 50;
findfactors($n);
      
// This code is contributed 
// by ChitraNayal
?>

chevron_right


Output:

All the factors are -> 1 2 5 10 25 50 
Product is -> 12500


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.