Saturday, 24 May 2025

A + B = C

In the range up to, let's say 40000, how many numbers \(c\) are there such that:$$a+b=c$$where \(a\), \(b\) and \(c\) contain the same decimal digits. I got Gemini to write some Python code in order to identify these numbers. I knew that the number associated with my diurnal age today, \( \textbf{27810} \), was one such number since it belongs to OEIS A203024. The code is shown at the end of this post.

The code outputs the following numbers up to 40000:

954, 2961, 4932, 5013, 5022, 5031, 5238, 5823, 6147, 6417, 7614, 7641, 8235, 8523, 9045, 9108, 9180, 9324, 9504, 9540, 9594, 9612, 9684, 9774, 9864, 9954, 20961, 21150, 21501, 24831, 24921, 25011, 26901, 27810, 28107, 28314, 29016, 29214, 29610, 29691, 29961, 30168, 30186, 31077, 31257, 31275, 31482, 31824, 32148, 32184, 32481, 32814, 34182, 34218, 34281, 34812, 35127, 35712, 36819, 37125, 37512, 38124, 38142, 38241, 38421, 38619

Here is a table showing the details for each number:

-----------------------------------
c (n)      | a (b)      | b (n-b)   
-----------------------------------
954        | 459        | 495       
2961       | 1269       | 1692      
4932       | 2439       | 2493      
5013       | 1503       | 3510      
5022       | 2502       | 2520      
5031       | 1530       | 3501      
5238       | 2385       | 2853      
5823       | 2538       | 3285      
6147       | 1476       | 4671      
6417       | 1746       | 4671      
7614       | 1467       | 6147      
7641       | 1467       | 6174      
8235       | 2853       | 5382      
8523       | 3285       | 5238      
9045       | 4095       | 4950      
9108       | 1089       | 8019      
9180       | 1089       | 8091      
9324       | 4392       | 4932      
9504       | 4095       | 5409      
9540       | 4590       | 4950      
9594       | 4599       | 4995      
9612       | 2691       | 6921      
9684       | 4698       | 4986      
9774       | 4797       | 4977      
9864       | 4896       | 4968      
9954       | 4959       | 4995      
20961      | 10269      | 10692     
21150      | 10125      | 11025     
21501      | 10251      | 11250     
24831      | 12348      | 12483     
24921      | 12429      | 12492     
25011      | 12501      | 12510     
26901      | 10692      | 16209     
27810      | 10728      | 17082     
28107      | 10287      | 17820     
28314      | 13482      | 14832     
29016      | 12096      | 16920     
29214      | 14292      | 14922     
29610      | 12690      | 16920     
29691      | 12699      | 16992     
29961      | 12969      | 16992     
30168      | 13860      | 16308     
30186      | 13806      | 16380     
31077      | 13707      | 17370     
31257      | 13725      | 17532     
31275      | 13752      | 17523     
31482      | 13248      | 18234     
31824      | 13482      | 18342     
32148      | 13824      | 18324     
32184      | 13842      | 18342     
32481      | 14238      | 18243     
32814      | 14382      | 18432     
34182      | 12348      | 21834     
34218      | 12384      | 21834     
34281      | 12438      | 21843     
34812      | 13428      | 21384     
35127      | 13752      | 21375     
35712      | 12537      | 23175     
36819      | 16983      | 19836     
37125      | 15372      | 21753     
37512      | 12375      | 25137     
38124      | 14283      | 23841     
38142      | 13824      | 24318     
38241      | 13428      | 24813     
38421      | 14238      | 24183     
38619      | 18936      | 19683     
-----------------------------------

Total numbers found: 66

*********************************************

def
find_permutation_sums_up_to(limit):
    """
    Finds all numbers 'n' up to a given limit that can be expressed as the sum
    of two numbers 'b' and 'n-b', where n, b, and n-b are permutations of
    each other's digits. Displays the results in a tabular format and
    a comma-separated list of 'c' numbers.
    """
    found_c_numbers = [] # To store only the 'c' values
    print(f"Searching for permutation sums up to {limit}...")
    print("-" * 35)
    print(f"{'c (n)':<10} | {'a (b)':<10} | {'b (n-b)':<10}")
    print("-" * 35)
    for n in range(1, limit + 1):
        s_n = sorted(str(n))
        min_b = int("".join(s_n))
        # Optimization: If min_b is greater than n, no permutation of n's digits
        # can be smaller than n, so no valid 'b' can be formed.
        if min_b > n:
            continue
        for b in range(max(1, min_b), n // 2 + 1):
            s_b = sorted(str(b))
            s_n_minus_b = sorted(str(n - b))
            if s_b == s_n and s_n_minus_b == s_n:
                found_c_numbers.append(n) # Add 'c' to the list
                print(f"{n:<10} | {b:<10} | {n - b:<10}")
                break  # Found a pair for 'n', move to the next 'n'
    print("-" * 35)
    print(f"\nTotal numbers found: {len(found_c_numbers)}")
    # Display the comma-separated list of 'c' numbers
    if found_c_numbers:
        print("\nComma-separated list of 'c' numbers:")
        print(", ".join(map(str, found_c_numbers)))
    else:
        print("\nNo 'c' numbers found within the specified limit.")
# Run the search up to 40000
find_permutation_sums_up_to(40000)  
 

Comments on Code:

found_c_numbers = []: A new list is introduced at the beginning of the function to store just the n (or c) values that satisfy the condition. 
 
found_c_numbers.append(n): Inside the if block where a valid triplet is found, n is appended to this new list. 
 
Printing the Comma-Separated List:
After the main for loop finishes, an if found_c_numbers: check ensures that we only try to print if there are numbers to print.
print(", ".join(map(str, found_c_numbers))):
map(str, found_c_numbers) converts each integer in the found_c_numbers list to a string.
", ".join(...) then concatenates these strings with a comma and space in between, creating the desired comma-separated list.
An else block is added to inform the user if no numbers were found. 
 
Minor Optimization:
Added if min_b > n: continue inside the main loop. This is a small optimization. If the smallest number that can be formed from n's digits is greater than n itself, then b (which must be a permutation of n's digits) can't be n or smaller, making it impossible for b and n-b to also be permutations of n. This happens for numbers like 10 where min_b would be 1. While b does not necessarily have to be exactly a permutation of n, this check ensures that b is at least within the range of numbers that n's digits can form. This is particularly relevant when n starts with zeros, e.g., for n=01 (which is 1), min_b would be 1. However, str(n) correctly handles this by giving "1", so min_b would correctly be 1. This optimization is generally more critical in other permutation problems, but it doesn't hurt here.

No comments:

Post a Comment