Encuentre cuatro factores de N con producto máximo y suma igual a N – Part 2

Dado un número entero  N. La tarea es encontrar todos los factores de N imprimir el producto de cuatro factores de N tal que: 

  • La suma de los cuatro factores es igual a N.
  • El producto de los cuatro factores es máximo.

Si no es posible encontrar 4 de esos factores, imprima «No es posible» .

Nota: Los cuatro factores pueden ser iguales entre sí para maximizar el producto.

Ejemplos

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.

En el segundo ejemplo, el producto será máximo cuando los cuatro factores sean iguales a 25. La suma de los cuatro factores es igual a ‘N’. Aunque podemos elegir el mismo factor cuatro veces para maximizar el producto.

A continuación se muestra el algoritmo paso a paso para resolver este problema:  

  1. Primero encuentre los factores de un número ‘N’ pasando de 1 a la raíz cuadrada de ‘N’ y compruebe si ‘i’ y ‘n/i’ dividen a N y almacénelos en un vector.
  2. Ordene el vector e imprima cada elemento.
  3. Encuentra tres números para maximizar el producto con el cuarto número, usando tres bucles.
  4. Reemplace el siguiente producto máximo con el producto anterior.
  5. Imprime el producto cuando encuentres los cuatro factores.

A continuación se muestra la implementación del enfoque anterior:  

C++

// 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 fourth 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;
}

Java

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 fourth 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

Python3

# 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 fourth 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

C#

// C# program to find four factors of N
// with maximum product and sum equal to N
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to find factors
    // and to print those four factors
    static void findfactors(int n)
    {
        List<int> vec = new List<int>();
 
        // 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
        vec.Sort();
 
        // print all the factors
        Console.WriteLine("All the factors are -> ");
        for (int i = 0; i < vec.Count; i++)
        {
            Console.Write(vec[i] + " ");
        }
        Console.WriteLine();
 
        // 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.Count; i++)
        {
            for (int j = i; j < vec.Count; j++)
            {
                for (int k = j; k < vec.Count; k++)
                {
                    // storing the fourth factor in y
                    int y = n - vec[i] - vec[j] -
                                         vec[k];
 
                    // if the fourth 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[i] * vec[j] *
                                              vec[k] * y, maxProduct);
                    }
                }
            }
        }
 
        // print the product if the numbers exist
        if (flag == false)
        {
            Console.WriteLine("Product is -> " +
                                    maxProduct);
        }
        else
        {
            Console.WriteLine("Not possible");
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n;
        n = 50;
        findfactors(n);
    }
}
 
// This code is contributed by Rajput-Ji

PHP

<?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 fourth 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
?>

Javascript

<script>
 
// Javascript 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)
    {
        let vec = [];
        // inserting all the factors in a vector s
        for (let i = 1; i * i <= n; i++)
        {
            if (n % i == 0)
            {
                vec.push(i);
                vec.push(Math.floor(n / i));
            }
        }
   
        // sort the vector
        vec.sort(function(a,b){return a-b;});
   
        // print all the factors
        document.write("All the factors are -> ");
        for (let i = 0; i < vec.length; i++)
        {
            document.write(vec[i] + " ");
        }
        document.write("<br>");
   
        // Any elements is divisible by 1
        let maxProduct = 1;
        let flag = true;
   
        // using three loop we'll find
        // the three maximum factors
        for (let i = 0; i < vec.length; i++)
        {
            for (let j = i; j < vec.length; j++)
            {
                for (let k = j; k < vec.length; k++)
                {
                    // storing the fourth factor in y
                    let y = n - vec[i] - vec[j] -
                                    vec[k];
   
                    // if the fourth 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[i] *
                        vec[j] * vec[k] * y, maxProduct);
                    }
                }
            }
        }
   
        // print the product if the numbers exist
        if (flag == false)
        {
            document.write("Product is -> " + maxProduct);
        }
        else
        {
            document.write("Not possible");
        }
    }
     
    // Driver code
    let n = 50;
    findfactors(n);
         
 
 
// This code is contributed by avanitrachhadiya2155
 
</script>
Producción: 

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

 

Publicación traducida automáticamente

Artículo escrito por AmanSrivastava1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *