Python Program for Find minimum sum of factors of number

In this article, we will learn how to find the minimum sum of factors of a given number. The key insight is that the minimum sum is achieved by using the sum of prime factors of the number.

Problem Statement

Given a number, find the minimum sum of factors that can multiply to give the original number. For example, the number 12 can be expressed as:

  • 12 = 12 (sum = 12)
  • 12 = 6 × 2 (sum = 8)
  • 12 = 4 × 3 (sum = 7)
  • 12 = 3 × 2 × 2 (sum = 7)

The minimum sum is 7, which comes from the prime factorization.

Algorithm Approach

Prime Factorization Process for Number 12 Start: 12 Check i = 2 12 ÷ 2 = 6 Add 2 to sum sum = 2 6 ÷ 2 = 3 Add 2 to sum sum = 4 Check i = 3 3 is prime Add 3 to sum Final Result sum = 2+2+3 = 7 Prime Factors: 2, 2, 3 ? Minimum Sum = 7

Implementation

Here's the iterative approach to find the minimum sum of factors ?

def findMinSum(num):
    sum_factors = 0
    
    # Find prime factors starting from 2
    i = 2
    while i * i <= num:
        # While i divides num, add i to sum and divide num by i
        while num % i == 0:
            sum_factors += i
            num //= i
        i += 1
    
    # If num is still greater than 1, it's a prime factor
    if num > 1:
        sum_factors += num
    
    return sum_factors

# Test the function
num = 12
result = findMinSum(num)
print(f"Minimum sum of factors for {num}: {result}")

# Test with more examples
test_numbers = [6, 18, 30, 13]
for n in test_numbers:
    print(f"Minimum sum of factors for {n}: {findMinSum(n)}")
Minimum sum of factors for 12: 7
Minimum sum of factors for 6: 5
Minimum sum of factors for 18: 8
Minimum sum of factors for 30: 10
Minimum sum of factors for 13: 13

How It Works

The algorithm works by finding all prime factors of the given number:

  1. Start with smallest prime (2): Check if the number is divisible by 2
  2. Extract all factors: Keep dividing by the current prime until no longer divisible
  3. Move to next potential prime: Increment and repeat the process
  4. Handle remaining prime: If a number greater than 1 remains, it's also a prime factor

Example Breakdown

For number 12:

  • 12 ÷ 2 = 6, add 2 to sum (sum = 2)
  • 6 ÷ 2 = 3, add 2 to sum (sum = 4)
  • 3 is prime, add 3 to sum (sum = 7)

Therefore, 12 = 2 × 2 × 3, and minimum sum = 2 + 2 + 3 = 7.

Time Complexity

The time complexity is O(?n) where n is the input number, as we only iterate up to the square root of n to find all prime factors.

Conclusion

The minimum sum of factors is achieved by using prime factorization. This approach efficiently finds all prime factors and returns their sum, which represents the minimum possible sum of factors for any given number.

Updated on: 2026-03-25T06:29:09+05:30

757 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements