Find four factors of N with maximum product and sum equal to N | Set 3
Given an integer N. The task is to find all factors of N and print the product of four factors of N such that:
- Sum of the four factors is equal to N.
- The 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 and there can be a large number of queries.
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.
The idea is to find factors of all numbers from 1 to N ( which is the maximum value of n ).
- An answer will be Not possible if the given is prime.
And if the given n is divisible by 4 then answer will be pow(q, 4) where q is a quotient when n is divided by 4.- If it is possible to find the answer then, the answer must include third last factor two times. And run a nested loop for other two factors.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
void factors( int N, vector< int >& v[])
{
for ( int i = 2; i < N; i++) {
for ( int j = 1; j * j <= i; j++) {
if (i % j == 0) {
if (i / j == j)
v[i].push_back(j);
else {
v[i].push_back(j);
v[i].push_back(i / j);
}
}
}
sort(v[i].begin(), v[i].end());
}
}
int product( int n)
{
vector< int > v[n + 100];
factors(n + 100, v);
if (n % 4 == 0) {
int x = n / 4;
x *= x;
return x * x;
}
else {
if (isPrime[n])
return -1;
else {
int ans = -1;
if (v[n].size() > 2) {
int fac = v[n][v[n].size() - 3];
for ( int i = v[n].size() - 1; i >= 0; i--) {
for ( int j = v[n].size() - 1; j >= 0; j--) {
if ((fac * 2) + (v[n][j] + v[n][i]) == n)
ans = max(ans, fac * fac * v[n][j] * v[n][i]);
}
}
return ans;
}
}
}
}
int main()
{
int n = 24;
cout << product(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
if (n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ; i * i <= n; i = i + 6 )
if (n % i == 0 || n % (i + 2 ) == 0 )
return false ;
return true ;
}
static Vector<Vector<Integer> > v = new Vector<Vector<Integer> >();
static void factors( int N )
{
for ( int i = 2 ; i < N; i++)
{
for ( int j = 1 ; j * j <= i; j++)
{
if (i % j == 0 )
{
if (i / j == j)
v.get(i).add(j);
else
{
v.get(i).add(j);
v.get(i).add(i / j);
}
}
}
Collections.sort(v.get(i));
}
}
static int product( int n)
{
v.clear();
for ( int i = 0 ; i < n + 100 ; i++)
v.add( new Vector<Integer>());
factors(n + 100 );
if (n % 4 == 0 )
{
int x = n / 4 ;
x *= x;
return x * x;
}
else
{
if (isPrime(n))
return - 1 ;
else
{
int ans = - 1 ;
if (v.get(n).size() > 2 )
{
int fac = v.get(n).get(v.get(n).size() - 3 );
for ( int i = v.get(n).size() - 1 ; i >= 0 ; i--)
{
for ( int j = v.get(n).size() - 1 ; j >= 0 ; j--)
{
if ((fac * 2 ) + (v.get(n).get(j) +
v.get(n).get(i)) == n)
ans = Math.max(ans, fac * fac *
v.get(n).get(j) *
v.get(n).get(i));
}
}
return ans;
}
}
}
return 0 ;
}
public static void main(String args[])
{
int n = 24 ;
System.out.println( product(n));
}
}
|
Python3
from math import sqrt, ceil, floor
def isPrime(n):
if (n < = 1 ):
return False
if (n < = 3 ):
return True
if (n % 2 = = 0 or n % 3 = = 0 ):
return False
for i in range ( 5 , ceil(sqrt(n)), 6 ):
if (n % i = = 0 or n % (i + 2 ) = = 0 ):
return False
return True
def factors(N, v):
for i in range ( 2 , N):
for j in range ( 1 ,ceil(sqrt(i)) + 1 ):
if (i % j = = 0 ):
if (i / / j = = j):
v[i].append(j)
else :
v[i].append(j)
v[i].append(i / / j)
v = sorted (v)
def product(n):
v = [[]] * (n + 100 )
factors(n + 100 , v)
if (n % 4 = = 0 ):
x = n / / 4
x * = x
return x * x
else :
if (isPrime[n]):
return - 1
else :
ans = - 1
if ( len (v[n]) > 2 ):
fac = v[n][ len (v[n]) - 3 ]
for i in range ( len (v[n] - 1 ), - 1 , - 1 ):
for j in range ( len (v[n] - 1 ), - 1 , - 1 ):
if ((fac * 2 ) + (v[n][j] + v[n][i]) = = n):
ans = max (ans, fac * fac * v[n][j] * v[n][i])
return ans
n = 24
print (product(n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
static List<List< int > > v = new List<List< int > >();
static void factors( int N )
{
for ( int i = 2; i < N; i++)
{
for ( int j = 1; j * j <= i; j++)
{
if (i % j == 0)
{
if (i / j == j)
v[i].Add(j);
else
{
v[i].Add(j);
v[i].Add(i / j);
}
}
}
v[i].Sort();
}
}
static int product( int n)
{
v.Clear();
for ( int i = 0; i < n + 100; i++)
v.Add( new List< int >());
factors(n + 100);
if (n % 4 == 0)
{
int x = n / 4;
x *= x;
return x * x;
}
else
{
if (isPrime(n))
return -1;
else
{
int ans = -1;
if (v[n].Count > 2)
{
int fac = v[n][v[n].Count - 3];
for ( int i = v[n].Count - 1; i >= 0; i--)
{
for ( int j = v[n].Count - 1; j >= 0; j--)
{
if ((fac * 2) + (v[n][j] +
v[n][i]) == n)
ans = Math.Max(ans, fac * fac *
v[n][j] *
v[n][i]);
}
}
return ans;
}
}
}
return 0;
}
public static void Main(String []args)
{
int n = 24;
Console.WriteLine( product(n));
}
}
|
Javascript
<script>
function isPrime(n) {
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for (let i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
function factors(N, v) {
for (let i = 2; i < N; i++) {
for (let j = 1; j * j <= i; j++) {
if (i % j == 0) {
if (i / j == j)
v[i].push(j);
else {
v[i].push(j);
v[i].push(i / j);
}
}
}
v.sort((a, b) => a - b);
}
}
function product(n) {
let v = new Array();
for (let i = 0; i < n + 100; i++) {
v.push( new Array())
}
factors(n + 100, v);
if (n % 4 == 0) {
let x = n / 4;
x *= x;
return x * x;
}
else {
if (isPrime[n])
return -1;
else {
let ans = -1;
if (v[n].length > 2) {
let fac = v[n][v[n].length - 3];
for (let i = v[n].length - 1; i >= 0; i--) {
for (let j = v[n].length - 1; j >= 0; j--) {
if ((fac * 2) + (v[n][j] + v[n][i]) == n)
ans = Math.max(ans, fac * fac * v[n][j] * v[n][i]);
}
}
return ans;
}
}
}
}
let n = 24;
document.write(product(n));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(n2)
Last Updated :
31 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...