Find the maximum number of composite summands of a number in Python

Given a number N (where 1 ? N ? 10^9), we need to represent N as a sum of the maximum possible number of composite numbers. A composite number is a positive integer greater than 1 that has at least one positive divisor other than 1 and itself (like 4, 6, 8, 9, 10, etc.).

For example, if N = 16, we can write it as 4 + 4 + 4 + 4 (4 summands) or 8 + 8 (2 summands). The maximum number of summands is 4.

Approach

We use dynamic programming with the smallest composite numbers: 4, 6, and 9. For large numbers, we can use the fact that any large number can be expressed using mostly 4's (since 4 is the smallest composite number).

Algorithm Steps

  • Create a lookup table for small numbers (up to 16) using dynamic programming

  • For each number, try adding composite numbers 4, 6, or 9

  • For large numbers, use mostly 4's with the precomputed remainder

Implementation

def pre_calc():
    max_val = 16
    table = [-1] * max_val
    table[0] = 0
    composites = [4, 6, 9]  # Smallest composite numbers
    
    for i in range(1, max_val):
        for composite in composites:
            if i >= composite and table[i - composite] != -1:
                table[i] = max(table[i], table[i - composite] + 1)
    
    return table

def max_composite_summands(n):
    table = pre_calc()
    max_val = 16
    
    if n < max_val:
        return table[n]
    else:
        # For large numbers, use mostly 4's
        t = (n - max_val) // 4 + 1
        return t + table[n - 4 * t]

# Test with example
n = 16
result = max_composite_summands(n)
print(f"Maximum composite summands for {n}: {result}")
Maximum composite summands for 16: 4

How It Works

The algorithm works in two phases:

Phase 1: Dynamic Programming Table

We build a table for numbers 0 to 15 using the smallest composite numbers (4, 6, 9):

# Show the lookup table
table = pre_calc()
print("Number -> Max Summands")
for i in range(len(table)):
    print(f"{i:2d} -> {table[i]:2d}" if table[i] != -1 else f"{i:2d} -> -1")
Number -> Max Summands
 0 ->  0
 1 -> -1
 2 -> -1
 3 -> -1
 4 ->  1
 5 -> -1
 6 ->  1
 7 -> -1
 8 ->  2
 9 ->  1
10 ->  2
11 -> -1
12 ->  2
13 ->  2
14 ->  2
15 ->  2

Phase 2: Large Numbers

For numbers ? 16, we use the formula: use as many 4's as possible and handle the remainder using our precomputed table.

Test with Multiple Examples

test_cases = [4, 6, 8, 9, 12, 16, 20, 100]

for num in test_cases:
    result = max_composite_summands(num)
    if result == -1:
        print(f"N = {num}: Cannot be expressed as sum of composites")
    else:
        print(f"N = {num}: Maximum {result} composite summands")
N = 4: Maximum 1 composite summands
N = 6: Maximum 1 composite summands
N = 8: Maximum 2 composite summands
N = 9: Maximum 1 composite summands
N = 12: Maximum 2 composite summands
N = 16: Maximum 4 composite summands
N = 20: Maximum 5 composite summands
N = 100: Maximum 25 composite summands

Key Points

  • Numbers 1, 2, 3, 5, 7, 11 cannot be expressed as sums of composite numbers

  • The algorithm uses 4, 6, and 9 as base composite numbers

  • For large numbers, using mostly 4's gives the maximum summands

  • Time complexity: O(1) after preprocessing

Conclusion

This algorithm efficiently finds the maximum number of composite summands by using dynamic programming for small numbers and a mathematical approach for larger ones. The key insight is that 4 (the smallest composite) maximizes the number of summands for most cases.

Updated on: 2026-03-25T09:31:39+05:30

336 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements