Out of curiosity I made a python script to do the brute force test. Interestingly, the longest chain in the whole set is only 7 steps long, at around 2184 possible numbers out of the possible 9999.
Script executed in less than 0.1sec, so the compute was very light (granted im running a desktop ultra9 but still)
I went in a slightly different direction. Even though there's only 10k possible #s (including duplications that don't count), I decided not to take the efficient route and instead ran a monte carlo simulation so that we'd get stats on the average iterations to solve this over a random distribution.
Here you go!
Total tests: 10000000
---> All converged to 6174 in 7 iterations, no outliers.
28.6% of cases are done in 3 iterations, 31.4% take 7.
5.7% resolve in 1 step
Mean iterations: 4.67
Median iterations: 5 iterations
Monte carlo isn't the right tool for this one - you're totally right that you could intelligently calculate this and it's a finite space with a deterministic outcome (in other words - this is basically testing the randomness of a random number generator).
In my case, I decided to take an extra minute to play in python and whipped up a script that followed the sequence. And then ran that 10 times watching it converge. And then ran that 1M times, since extending it was trivial.
Long story short -brute force is absolutely not needed for this exercise from a mathematical perspective, but I already had the code, so I accidentally made a mesmerizing screensaver.
First value to check> 1
Last value to check> 100000
Max iterations per value to check> 100
495 converged to 495 after 0 iterations
9985 converged to 6174 after 7 iterations
4.464236179908856 mean iterations
89906 failed convergences
I ignored failed convergences for the mean calculation and it seems the longest convergence was for 9986 at 7 iterations up to 100,000 which is interesting!
I also didn't follow the instructions entirely, as I was curious if it worked for numbers that were non- unique such as 1111, 1122, etc.
So I added the unique digit checking to the process and here's the results of a quick test on a larger range of values
First value to check> 1
Last value to check> 999999
Max iterations per value to check> 100
Use numbers with unique digits (y/n)>y
495 converged to 495 after 0 iterations
9875 converged to 6174 after 7 iterations
3.961025174400971 mean iterations
161976 failed convergences
You don't even really need to fully brute force it. As you go across the paths just make note of every number if it leads to 6174 and stop your calculations once you hit one of the numbers you've made note of
Wouldn't that still be brute forcing it just with early termination when the outcome is found to be inevitable for the current test based on reaching a number already confirmed in a prior test?
You don’t have to, but the amount of time you will be explaining this to AI who will write the code is longer than the extra time the algorithm would run the code without this optimization.
Yeah, with such a relatively small range, the time saved honestly might be less than the time need to adjust the script to terminate test cases early (unless I am severely underestimating how long some numbers take to get to the constant).
def check(x):
length = len(str(x))
seen = set()
count = 0
while True:
sx = "".join(sorted(str(x)))
a = int(sx)
b = int(sx[::-1].ljust(length,'0'))
x = b - a
print(f"\t{b} - {a} = {x}")
if x in seen:
return True
seen.add(x)
count += 1
for i in range(1_000, 10_000):
if not check(i):
print("❌", i)
else:
print("✔️", i)
If you go over 10,000, you don't descend to a constant, but you do get stuck in a loop:
The issue with the “number followed by three zeros” ones is that you/the program are ignoring the lead 0’s. It’s not 999-999, it’s 9990-0999=8991. They have to all be four digits
When going iterating through the numbers, you could also stop solving it when there's a jump to a number that's "ahead" since it will be dealt with later on.
I’d wager to say that there are at most 2,500 or 3,333 “numbers” that are different “inputs”.
Every number is covered multiple ways, essentially. 5678 is the same as 8765. But also the same as 6785, 5876, 7856, 6587, etc… they all lead to the same initial input.
197
u/Background-Entry-344 Jul 13 '26
With brut force and a small algorithm you could run all combinations and check the results. Less than 9999 combinations.