Building on my previous A + B = C post, I decided to get Gemini to modify the code to find all numbers such that:
4617, 4851, 5103, 5184, 5913, 6021, 6129, 6192, 6219, 6291, 6921, 7182, 7281, 7416, 7614, 8145, 8154, 8253, 8325, 8451, 8514, 8523, 8541, 9135, 9216, 9234, 9324, 9612, 9621, 31860, 31905, 36171, 36711, 37116, 37161, 38061, 38106, 38151, 38214, 38511, 39051, 39105, 39411, 39501
This sequence is not listed in the OEIS. Note the gap between 9621 and 31680. Here is a table showing the details for each of these numbers:
--------------------------------------------------
d | a | b | c
--------------------------------------------------
4617 | 1467 | 1476 | 1674
4851 | 1458 | 1548 | 1845
5103 | 1035 | 1053 | 3015
5184 | 1485 | 1845 | 1854
5913 | 1359 | 1395 | 3159
6021 | 1260 | 2160 | 2601
6129 | 1269 | 2169 | 2691
6192 | 1269 | 1962 | 2961
6219 | 1296 | 1962 | 2961
6291 | 1926 | 2169 | 2196
6921 | 1269 | 2691 | 2961
7182 | 2178 | 2187 | 2817
7281 | 1782 | 2718 | 2781
7416 | 1476 | 1764 | 4176
7614 | 1467 | 1476 | 4671
8145 | 1485 | 1845 | 4815
8154 | 1458 | 1548 | 5148
8253 | 2385 | 2583 | 3285
8325 | 2358 | 2385 | 3582
8451 | 1458 | 1845 | 5148
8514 | 1485 | 1548 | 5481
8523 | 2358 | 2583 | 3582
8541 | 1548 | 1845 | 5148
9135 | 1593 | 3591 | 3951
9216 | 1296 | 1629 | 6291
9234 | 2349 | 2493 | 4392
9324 | 2439 | 2493 | 4392
9612 | 1629 | 1692 | 6291
9621 | 1296 | 2196 | 6129
31860 | 10386 | 10638 | 10836
31905 | 10359 | 10593 | 10953
36171 | 11367 | 11637 | 13167
36711 | 11367 | 11673 | 13671
37116 | 11637 | 11763 | 13716
37161 | 11637 | 11763 | 13761
38061 | 10368 | 10863 | 16830
38106 | 10638 | 13608 | 13860
38151 | 11385 | 11583 | 15183
38214 | 12348 | 12384 | 13482
38511 | 11358 | 11835 | 15318
39051 | 10953 | 13059 | 15039
39105 | 10953 | 13059 | 15093
39411 | 11349 | 13149 | 14913
39501 | 10539 | 13059 | 15903
--------------------------------------------------
Total 'd' numbers found: 44
******************************************************
def find_permutation_sums_of_three_unique_up_to(limit):
"""
Finds all numbers 'd' up to a given limit such that there exist three
UNIQUE numbers 'a', 'b', and 'c' (where a < b < c) such that a + b + c = d,
and a, b, c, d are all permutations of each other's digits.
Displays the results in a tabular format and a comma-separated list of 'd' numbers.
"""
found_d_numbers = []
print(f"Searching for unique permutation sums (a + b + c = d, with a < b < c) up to {limit}...")
print("-" * 50)
print(f"{'d':<10} | {'a':<10} | {'b':<10} | {'c':<10}")
print("-" * 50)
for d in range(1, limit + 1):
s_d = sorted(str(d)) # Canonical representation of d's digits
# Smallest number that can be formed from d's digits
min_val_from_digits = int("".join(s_d))
# Optimization: If d is too small, it's impossible to form d from three positive permutations
# The smallest sum of three *unique* positive permutations of d's digits would be at least
# min_val + (min_val + 1) + (min_val + 2) if they are distinct and permutable.
# A simpler lower bound for unique permutations is 3 * min_val_from_digits.
if d < 3 * min_val_from_digits:
continue
# Another strong optimization: d must have at least 3 numbers that are permutations of its digits.
# This is implicitly handled by the loops, but it's good to keep in mind.
# Iterate for 'a'
# 'a' can go up to d // 3 (roughly). Must be at least min_val_from_digits.
for a in range(max(1, min_val_from_digits), d // 3 + 1):
s_a = sorted(str(a))
if s_a != s_d:
continue
remaining_sum_bc = d - a
# Optimization: The remaining sum must be greater than 2 * a for b and c to be distinct and larger than a.
# Also, remaining_sum_bc must be large enough for two distinct permutations.
if remaining_sum_bc <= 2 * a or remaining_sum_bc < 2 * min_val_from_digits:
continue
# Iterate for 'b'
# 'b' must be greater than 'a'.
# 'b' can go up to remaining_sum_bc // 2 (roughly).
for b in range(a + 1, remaining_sum_bc // 2 + 1): # Start 'b' from a + 1
s_b = sorted(str(b))
if s_b != s_d:
continue
c = remaining_sum_bc - b
# Check for uniqueness and order: c must be greater than b
if c <= b: # c must be greater than b to satisfy a < b < c
continue
s_c = sorted(str(c))
# Final check: c is a permutation of d's digits
if s_c == s_d:
found_d_numbers.append(d)
print(f"{d:<10} | {a:<10} | {b:<10} | {c:<10}")
# Found a valid (a,b,c) for this 'd', move to the next 'd'
break # Break from 'b' loop
else:
continue # If no 'b' was found for this 'a', continue to next 'a'
break # Break from 'a' loop if a solution was found for 'b' (and implicitly 'c')
else:
continue # If no 'a' was found, continue to the next 'd'
print("-" * 50)
print(f"\nTotal 'd' numbers found: {len(found_d_numbers)}")
if found_d_numbers:
print("\nComma-separated list of 'd' numbers:")
print(", ".join(map(str, sorted(list(set(found_d_numbers))))))
else:
print("\nNo 'd' numbers found within the specified limit.")
# Run the search up to 40000
find_permutation_sums_of_three_unique_up_to(40000)
Key Changes to Enforce a < b < c:
Loop for b starts from a + 1:
Original: for b in range(max(a, min_val_from_digits), ...)
New: for b in range(a + 1, ...)
This directly enforces b > a. Since a is already at least min_val_from_digits, b will also be.
Check for c > b:
Original if s_c == s_d and c >= b:
New: if c <= b: continue (Moved to an early check)
Then if s_c == s_d: (Final check, as c > b is already ensured)
After calculating c, we immediately check if c <= b: continue. This ensures that c is strictly greater than b. If c is not greater than b, this (a, b, c) triplet doesn't satisfy the a < b < c condition, so we move to the next b.
Refined Loop Bounds & Optimizations:
b's upper bound: remaining_sum_bc // 2 + 1 is still appropriate, as b must be less than c (and b+c = remaining_sum_bc).
Early exit for remaining_sum_bc: if remaining_sum_bc <= 2 * a or remaining_sum_bc < 2 * min_val_from_digits:
remaining_sum_bc <= 2 * a: If b + c is not greater than 2 * a, then it's impossible for b > a and c > b.
remaining_sum_bc < 2 * min_val_from_digits: Still valid, as b and c must be at least min_val_from_digits.Impact of a < b < c:
Fewer Results: You will find significantly fewer d numbers that satisfy this stricter condition compared to the previous version where a, b, c could be equal or in any order.
Faster Execution: The a < b < c constraint prunes the search space considerably. By skipping a=b, b=c, a=c, and permutations, the inner loops will run fewer iterations, making the search faster.This version is more aligned with typical "unique permutation sum" problems
No comments:
Post a Comment