### Random Post

### Recent Posts

### Recent Comments

### Archives

### Categories

- article (11)
- enigma (1,115)
- misc (2)
- project euler (2)
- puzzle (29)
- site news (43)
- tantalizer (29)
- teaser (3)

### Site Stats

- 166,357 hits

Programming Enigma Puzzles

8 January 2016

Posted by on **From New Scientist #1475, 26th September 1985** [link]

Below is an addition sum with letters substituted for digits. The same letter stands for the same digit wherever it appears, and different letters stand for different digits.

Write out the sum with numbers substituted for letters.

[enigma327]

Advertisements

%d bloggers like this:

This is another problem that can be fed directly into the

SubstitutedSum()solver from theenigma.pylibrary.This Python program runs in 52ms.

Solution:1912803 + 2428850 + 4312835 = 8654488.Instead of writing individual programs for such problems, I have added code to the

enigma.pylibrary that allows you call theSubstitutedSum()solver directly from the command line. This kind of problem can now be solved directly by just passing the parameters as command line arguments:Here is a MiniZinc solution;

Easier to programme as a Python permutation solution, but not as fast as MiniZinc

Hi so I had a go at the problem (sorry I’m quite new to python lol!!) and I made this

but it seems to be running a bit slowly can anyone help me??? Thanks again for the help!!

Hi Liam – Welcome to Enigmatic Code.

Your program produces the right answer, and on my machine it runs in about 1.2s (using PyPy 4.0.1, or about 19.4s using the standard Python 2.7.11 interpreter). The reason it’s slow is the simplicity of the algorithm. The nested

forloops construct all possible assignments of the digits to the letters, so the test at line 18 is evaluated 3,628,800 times (=factorial(10)).Three years ago, after coding up a solution to

Enigma 63– a similar problem – (using Python’sitertools.permutations()to generate all possible assignments of letters to digits), I decided to write a generic solver for such puzzles to save me the bother of having to write a custom program each time. (Puzzles like this were quite popular in early Enigmas). I ended up with a more sophisticated algorithm that does many fewer tests, and I’ve incorporated that into theSubstitutedSum()solver in theenigma.pylibrary. On this problem it runs about 1000× faster than your program (using the standard Python interpreter).Oh gosh I feel a bit slow now – who knew we we’re supposed to build fast programs and not just simple programmes! gosh 1000 x faster I see I have a lot of learning to do! I’m quite new to all this python stuff, sorry haha. Although I suppose if you’ve already written a computer to solve all these sorts of problems there’s not much point me writing one!! I’ll have a go at some of the other problems here then, thanks again for the help!

I love your code. For me it symbolises a lot of my feelings about programming as an art form: a perfect merging of mechanical beauty and gritty complexity. Sure, it’s not concise, but it exaggerates and elucidates the actual physicality of what is required to calculate a solution. Bravissimo.

Haha gosh that’s so nice of you fisty frodo (or should I call you fisty?) theres clearly a fierce debate here lol! You used some difficult words, but I think I know what you meant! Bravissimo is Spanish right? Yeah so maybe it’s time for programmers to write computers that are not fast but sort of more simple and I’m sure people would appreciate that! Thanks again!