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 | 4952961 | 1269 | 16924932 | 2439 | 24935013 | 1503 | 35105022 | 2502 | 25205031 | 1530 | 35015238 | 2385 | 28535823 | 2538 | 32856147 | 1476 | 46716417 | 1746 | 46717614 | 1467 | 61477641 | 1467 | 61748235 | 2853 | 53828523 | 3285 | 52389045 | 4095 | 49509108 | 1089 | 80199180 | 1089 | 80919324 | 4392 | 49329504 | 4095 | 54099540 | 4590 | 49509594 | 4599 | 49959612 | 2691 | 69219684 | 4698 | 49869774 | 4797 | 49779864 | 4896 | 49689954 | 4959 | 499520961 | 10269 | 1069221150 | 10125 | 1102521501 | 10251 | 1125024831 | 12348 | 1248324921 | 12429 | 1249225011 | 12501 | 1251026901 | 10692 | 1620927810 | 10728 | 1708228107 | 10287 | 1782028314 | 13482 | 1483229016 | 12096 | 1692029214 | 14292 | 1492229610 | 12690 | 1692029691 | 12699 | 1699229961 | 12969 | 1699230168 | 13860 | 1630830186 | 13806 | 1638031077 | 13707 | 1737031257 | 13725 | 1753231275 | 13752 | 1752331482 | 13248 | 1823431824 | 13482 | 1834232148 | 13824 | 1832432184 | 13842 | 1834232481 | 14238 | 1824332814 | 14382 | 1843234182 | 12348 | 2183434218 | 12384 | 2183434281 | 12438 | 2184334812 | 13428 | 2138435127 | 13752 | 2137535712 | 12537 | 2317536819 | 16983 | 1983637125 | 15372 | 2175337512 | 12375 | 2513738124 | 14283 | 2384138142 | 13824 | 2431838241 | 13428 | 2481338421 | 14238 | 2418338619 | 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 sumof two numbers 'b' and 'n-b', where n, b, and n-b are permutations ofeach other's digits. Displays the results in a tabular format anda comma-separated list of 'c' numbers."""found_c_numbers = [] # To store only the 'c' valuesprint(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:continuefor 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 listprint(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' numbersif 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 40000find_permutation_sums_up_to(40000)
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.Comments on Code:
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