Python program to find better divisor of a number

Finding the "better divisor" of a number involves comparing divisors based on their digit sum. A divisor with a higher digit sum is considered better, and if digit sums are equal, the smaller number wins.

Problem Definition

Given a number n, we need to find the best divisor based on these criteria ?

  • The divisor with the highest sum of digits is better
  • If digit sums are equal, the smaller divisor is better

Example

For n = 180, the divisors are [1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 30, 36, 45, 60, 90, 180]. The divisors with maximum digit sum (9) are [9, 18, 36, 45, 90, 180], and among them 9 is the smallest.

Algorithm Steps

The approach follows these steps ?

  • Initialize best divisor as 1 and maximum digit sum as 1
  • For each number from 2 to n, check if it's a divisor
  • Calculate the sum of digits for each divisor
  • Update the best divisor if current digit sum is greater

Implementation

def solve(n):
    best_div = 1
    max_digit_sum = 1
    
    for i in range(2, n + 1):
        # Check if i is a divisor of n
        if n % i != 0:
            continue
            
        # Calculate sum of digits
        k = i
        digit_sum = 0
        while k > 0:
            digit_sum += k % 10
            k //= 10
            
        # Update best divisor if current digit sum is greater
        if digit_sum > max_digit_sum:
            max_digit_sum = digit_sum
            best_div = i
    
    return best_div

# Test with example
n = 180
result = solve(n)
print(f"Better divisor of {n}: {result}")

# Let's also see all divisors and their digit sums for verification
def get_all_divisors_with_sums(n):
    divisors = []
    for i in range(1, n + 1):
        if n % i == 0:
            digit_sum = sum(int(digit) for digit in str(i))
            divisors.append((i, digit_sum))
    return divisors

divisors_info = get_all_divisors_with_sums(180)
print("All divisors with their digit sums:")
for div, ds in divisors_info:
    print(f"Divisor: {div}, Digit Sum: {ds}")
Better divisor of 180: 9
All divisors with their digit sums:
Divisor: 1, Digit Sum: 1
Divisor: 2, Digit Sum: 2
Divisor: 3, Digit Sum: 3
Divisor: 4, Digit Sum: 4
Divisor: 5, Digit Sum: 5
Divisor: 6, Digit Sum: 6
Divisor: 9, Digit Sum: 9
Divisor: 10, Digit Sum: 1
Divisor: 12, Digit Sum: 3
Divisor: 15, Digit Sum: 6
Divisor: 18, Digit Sum: 9
Divisor: 20, Digit Sum: 2
Divisor: 30, Digit Sum: 3
Divisor: 36, Digit Sum: 9
Divisor: 45, Digit Sum: 9
Divisor: 60, Digit Sum: 6
Divisor: 90, Digit Sum: 9
Divisor: 180, Digit Sum: 9

Optimized Version

We can optimize by only checking divisors up to ?n and their corresponding pairs ?

import math

def solve_optimized(n):
    def digit_sum(num):
        return sum(int(digit) for digit in str(num))
    
    best_div = 1
    max_digit_sum = 1
    
    # Check all divisors
    for i in range(1, int(math.sqrt(n)) + 1):
        if n % i == 0:
            # Check divisor i
            ds1 = digit_sum(i)
            if ds1 > max_digit_sum:
                max_digit_sum = ds1
                best_div = i
            
            # Check corresponding divisor n//i (if different)
            if i != n // i:
                other_div = n // i
                ds2 = digit_sum(other_div)
                if ds2 > max_digit_sum:
                    max_digit_sum = ds2
                    best_div = other_div
                elif ds2 == max_digit_sum and other_div < best_div:
                    best_div = other_div
    
    return best_div

# Test optimized version
print(f"Optimized result: {solve_optimized(180)}")
Optimized result: 9

Key Points

  • Digit Sum Calculation: Convert to string and sum individual digits, or use modulo arithmetic
  • Tie Breaking: When digit sums are equal, choose the smaller divisor
  • Optimization: Check divisors up to ?n for better time complexity

Conclusion

The better divisor algorithm finds the divisor with maximum digit sum, choosing the smallest one in case of ties. The optimized approach reduces time complexity by checking only divisors up to ?n.

Updated on: 2026-03-26T15:34:54+05:30

499 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements