Maximum factors formed by two numbers in Python

We are given an array of integers and need to find the maximum number of factors formed by multiplying any two distinct numbers from the array. This involves calculating all possible products of pairs and then counting factors for each product.

Problem Understanding

Example 1

For array [3, 2, 10] ?

Input: [3, 2, 10]
Products: 3×2=6, 3×10=30, 2×10=20
Factors: 6?{1,2,3,6} (4 factors), 30?{1,2,3,5,6,10,15,30} (8 factors), 20?{1,2,4,5,10,20} (6 factors)
Output: Maximum factors = 8

Example 2

For array [1, 4, 6] ?

Input: [1, 4, 6]
Products: 1×4=4, 1×6=6, 4×6=24
Factors: 4?{1,2,4} (3 factors), 6?{1,2,3,6} (4 factors), 24?{1,2,3,4,6,8,12,24} (8 factors)
Output: Maximum factors = 8

Algorithm

  1. Calculate all products of distinct pairs from the array
  2. Count factors for each product
  3. Return the maximum factor count

Python Implementation

def count_factors(n):
    """Count the number of factors of a given number"""
    count = 0
    for i in range(1, n + 1):
        if n % i == 0:
            count += 1
    return count

def max_factors_from_pairs(arr):
    """Find maximum factors formed by multiplying two numbers"""
    max_factors = 0
    
    # Generate all pairs and calculate their products
    for i in range(len(arr)):
        for j in range(len(arr)):
            if i != j:  # Ensure distinct elements
                product = arr[i] * arr[j]
                factors = count_factors(product)
                max_factors = max(max_factors, factors)
    
    return max_factors

# Test with example 1
arr1 = [3, 2, 10]
result1 = max_factors_from_pairs(arr1)
print(f"Array: {arr1}")
print(f"Maximum factors formed by two numbers are: {result1}")

# Test with example 2
arr2 = [1, 4, 6]
result2 = max_factors_from_pairs(arr2)
print(f"Array: {arr2}")
print(f"Maximum factors formed by two numbers are: {result2}")
Array: [3, 2, 10]
Maximum factors formed by two numbers are: 8
Array: [1, 4, 6]
Maximum factors formed by two numbers are: 8

Optimized Implementation

For better performance when counting factors, we can optimize the factor counting function ?

import math

def count_factors_optimized(n):
    """Optimized function to count factors"""
    count = 0
    sqrt_n = int(math.sqrt(n))
    
    for i in range(1, sqrt_n + 1):
        if n % i == 0:
            if i * i == n:
                count += 1  # Perfect square
            else:
                count += 2  # Two factors: i and n/i
    
    return count

def max_factors_optimized(arr):
    """Optimized version with better factor counting"""
    max_factors = 0
    best_product = 0
    
    for i in range(len(arr)):
        for j in range(i + 1, len(arr)):  # Avoid duplicate pairs
            product = arr[i] * arr[j]
            factors = count_factors_optimized(product)
            if factors > max_factors:
                max_factors = factors
                best_product = product
    
    print(f"Best product: {best_product} with {max_factors} factors")
    return max_factors

# Test the optimized version
arr = [3, 2, 10]
result = max_factors_optimized(arr)
print(f"Maximum factors: {result}")
Best product: 30 with 8 factors
Maximum factors: 8

Detailed Factor Analysis

def analyze_products(arr):
    """Detailed analysis of all products and their factors"""
    products = []
    
    for i in range(len(arr)):
        for j in range(i + 1, len(arr)):
            product = arr[i] * arr[j]
            factors = count_factors_optimized(product)
            products.append((arr[i], arr[j], product, factors))
    
    # Sort by factor count in descending order
    products.sort(key=lambda x: x[3], reverse=True)
    
    print("Product Analysis:")
    print("Num1 × Num2 = Product (Factors)")
    for num1, num2, product, factor_count in products:
        print(f"{num1} × {num2} = {product} ({factor_count} factors)")
    
    return products[0][3]  # Return maximum factors

# Analyze the example
arr = [3, 2, 10]
max_factors = analyze_products(arr)
print(f"\nMaximum factors: {max_factors}")
Product Analysis:
Num1 × Num2 = Product (Factors)
3 × 10 = 30 (8 factors)
2 × 10 = 20 (6 factors)
2 × 3 = 6 (4 factors)

Maximum factors: 8

Conclusion

To find the maximum factors from pair products, calculate all possible products of distinct pairs, count factors for each product, and return the maximum count. The optimized factor counting using square root reduces time complexity from O(n) to O(?n) for each number.

Updated on: 2026-03-25T09:14:58+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements