Find four factors of N with maximum product and sum equal to N - Set-2 in Python Program

Given a number N, we need to find four factors of N such that their sum equals N and their product is maximized. The factors can be repeated to achieve the maximum product.

So, if the input is N = 60, then the output will be: All factors are [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60] and the maximum product is 50625, where 15 is selected four times (15 + 15 + 15 + 15 = 60 and 15 × 15 × 15 × 15 = 50625).

Algorithm Steps

To solve this problem, we follow these steps ?

  • Find all factors of N by checking divisibility up to ?N

  • Sort the factors in ascending order

  • Use three nested loops to select three factors

  • Calculate the fourth factor as: y = N - (sum of three factors)

  • Check if y is a valid factor of N

  • Track the maximum product among all valid combinations

Example Implementation

Let us see the following implementation to get better understanding ?

from math import sqrt

def get_factors(n):
    factors = []
    
    # Find all factors of n
    for i in range(1, int(sqrt(n)) + 1):
        if n % i == 0:
            factors.append(i)
            if i != n // i:  # Avoid duplicates for perfect squares
                factors.append(n // i)
    
    factors.sort()
    print("Factors are:", factors)
    
    final_prod = 0
    flag = 0
    
    # Try all combinations of three factors
    for i in range(len(factors)):
        for j in range(i, len(factors)):
            for k in range(j, len(factors)):
                # Calculate the fourth factor
                y = n - factors[i] - factors[j] - factors[k]
                
                if y <= 0:
                    break
                
                # Check if y is a factor of n
                if n % y == 0:
                    flag = 1
                    product = factors[i] * factors[j] * factors[k] * y
                    final_prod = max(product, final_prod)
    
    if flag == 1:
        print("Maximum product is:", final_prod)
    else:
        print("Not possible")

# Test the function
n = 60
get_factors(n)

The output of the above code is ?

Factors are: [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]
Maximum product is: 50625

How It Works

The algorithm works by:

  • Factor Finding: We iterate from 1 to ?N and add both i and N/i as factors when i divides N

  • Combination Testing: We use three nested loops to try all possible combinations of three factors

  • Fourth Factor Calculation: For each combination, we calculate y = N - (sum of three factors)

  • Validation: We check if y is positive and divides N evenly

  • Optimization: We track the maximum product found among all valid combinations

Testing with Another Example

# Test with N = 20
n = 20
get_factors(n)
Factors are: [1, 2, 4, 5, 10, 20]
Maximum product is: 625

For N = 20, the optimal solution is 5 + 5 + 5 + 5 = 20 with product 5 × 5 × 5 × 5 = 625.

Conclusion

This algorithm efficiently finds four factors of N that sum to N while maximizing their product. The key insight is that factors can be repeated, and often the maximum product is achieved by using the same factor multiple times, particularly factors close to N/4.

Updated on: 2026-03-25T09:50:02+05:30

211 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements