I don't know why Pete believes the largest 3-digit number is 900.
(In the final analysis, it turns out that he is right. The largest 3-digit number is 879. But how can we know that a priori?)
Since each digit must be unique, I believe the range of 3-digit numbers is 203 to 987, noting that the minimum 4-digit sum is 1023 and the maximum sum is 1640 (987+653). That is, 1 is always the most-significant digit of the 4-digit sum.
So, the max number of brute-force iterations is 616,225 (987-203+1 = 785, squared).
However, the inner loop can be conceptually y=MAX(1023-x0, 203) to 987. That reduces the number of brute-force iterations to 425,572.
I assume that 246+789 = 1035 is the same as 789+246 = 1035. So, I believe the number of solutions is 48, not 96.
The VBA procedure below finds the 48 solutions in less than 0.15 sec on my computer. (YMMV) I don't know if it qualifies as ``more efficient algorithm that works a little nicer and faster``.
If you want to count "duplicates", change #If 1 to #If 0. Ironically, it takes the same amount of time to find 96 solutions.
-----
That said, I believe an optimal algorithm requires at most 47,040 iterations. That is 8*7*7*6*5*4. 8*7 because the first digit of each 3-digit number is 2 to 9, and they are unique. 7*6*5*4 because the subsequent digits can include 0, and again, they are unique.
But I suspect that implementation is messier. (TBD)
-----
My brute-force implementation....
Errata.... Change Me.UsedRange.Clear to ActiveSheet.UsedRange.Clear if you put the procedure into a normal module instead of a worksheet module, as I did.
Bookmarks