Random Post
Recent Posts
- Enigma 767: Safety in numbers
- BrainTwister #18: The arithmetical two-step
- Enigma 774: Sting in the tail
- BrainTwister #17: Semi-one numbers
- Enigma 773: Duodecimal
- BrainTwister #16: Order, order!
- Enigma 772: Have you seen the trailer?
- BrainTwister #15: Domino strips
- Enigma 771: Cross the island
- BrainTwister #14: Factor graphs
Recent Comments
Archives
Categories
- article (11)
- braintwister (18)
- enigma (1,717)
- enigma-book-1982 (70)
- headscratchers-book-2023 (70)
- microteasers-book-1986 (11)
- misc (7)
- project euler (2)
- puzzle (90)
- puzzle# (249)
- site news (83)
- sphinx (4)
- tantalizer (254)
- tantalizer-book-1970 (40)
- teaser (7)
- today (1)
Site Stats
- 347,152 hits
The following Python program tries all permutations. It runs in 1.1s.
Solution: The sum is 8308440 + 8333218 + 8302040 + 8333260 = 33276958.
Having done a couple of these sums with letters substituted for digits recently I thought it would be fun to write a general solver for them.
The following code takes a straightforward right-to-left approach on the columns of the sum, recursively examining the possibilities for each column. There are three interfaces to it:
_substituted_sum()
is the core of the algorithm, but it assumes its arguments are nicely formed.substituted_sum()
is a friendlier way to run the algorithm, it makes sure the arguments are valid and fills out sensible default values.SubstitutedSum()
wraps the previous function as a class so you don’t have to remember the terms in sum itself, and provides some handy functions for reporting solutions.Gratifyingly this solution runs in 139ms, nearly 10× faster than the previous solution.
I will probably clean this up, add some more documentation and put it in the enigma.py library for future use.
I have added code to the enigma.py library to allow the (experimental) [[
SubstitutedExpression.split_sum
]] helper to be invoked from a run file (or the command line).The following run file executes in 66ms, and the generated program has an internal runtime of just 185µs. Which is over 2000× faster than my original program that just tries all possible assignments of digits to symbols.
Run: [ @replit ]