# Enigmatic Code

Programming Enigma Puzzles

## Enigma 220: Power mad

From New Scientist #1366, 14th July 1983 [link]

Not another mad letters-for-digits puzzle? Yes, but even crazier than usual. As always, different letters stand for different digits, and the same letter stands for the same digit from beginning to end. But this powerful example is quite mad, for example: ENIGMA = MAD.

As a further prime example of silliness, may I tell you too that PRIME is prime?

Please sort out my MIND.

Note: I am waiting for a phone line to be connected at my new house, so I only have sporadic access to the internet at the moment.

[enigma220]

### 9 responses to “Enigma 220: Power mad”

1. Jim Randell 6 September 2014 at 2:32 pm

This Python program runs in 32ms.

```from itertools import permutations
from enigma import nconcat, split, irange, is_prime, printf

digits = set(irange(0, 9))

for (M, A, D) in permutations(digits, 3):
MA = nconcat(M, A)
(ENIG, MA2) = divmod(MA ** D, 100)
if MA2 != MA: continue
if not(999 < ENIG < 10000): continue
(E, N, I, G) = split(ENIG, int)
ds = set((E, N, I, G, M, A, D))
if len(ds) != 7: continue

MIND = nconcat(M, I, N, D)
for (P, R) in permutations(digits.difference(ds), 2):
if P == 0: continue
PRIME = nconcat(P, R, I, M, E)
if not is_prime(PRIME): continue

printf("MIND={MIND} [ENIGMA={ENIG}{MA}, D={D}, PRIME={PRIME}]")
```

Solution: MIND = 2094.

MA = 25, D = 4, ENIGMA = 390625.

There are two possibilities for PRIME, 71023 and 81023.

• Jim Randell 26 July 2016 at 3:53 pm

This problem can be solved using the generalised Alphametic solver (SubstitutedExpression()) in the enigma.py library, and it also demonstrates the use of the answer parameter I recently added to it, which allows an expression to be specified that is also substituted and the number of solutions is counted according to the value of that expression:

```% python -m enigma SubstitutedExpression \
"MA ** D = ENIGMA" \
"is_prime(PRIME)"
(MA ** D = ENIGMA) (is_prime(PRIME))
(25 ** 4 = 390625) (is_prime(71023)) / A=5 D=4 E=3 G=6 I=0 M=2 N=9 P=7 R=1 / 2094
(25 ** 4 = 390625) (is_prime(81023)) / A=5 D=4 E=3 G=6 I=0 M=2 N=9 P=8 R=1 / 2094
MIND = 2094 [2 solutions]
```

This runs in 75ms.

2. geoffrounce 6 September 2014 at 5:16 pm
```from enigma import is_prime
from itertools import permutations

fs = 'MIND={}, MA={}, D={}, ENIGMA={}, PRIME={}'

for p1 in permutations('1234567890',7):
e,n,i,g,m,a,d = p1
if e == '0' or m == '0' or d == '0': continue
enigma = int(e + n + i + g + m + a)
ma = int(m + a)
mind = int(m + i + n + d)
d = int(d)
q1 = set('1234567890').difference(p1)
for z in permutations(q1,2):
p,r = z
if p == '0': continue
prime = int(p + r + i + m + e)
if enigma == ma**d and is_prime(prime):
print(fs.format(mind,ma,d,enigma,prime))

# MIND=2094, MA=25, D=4, ENIGMA=390625, PRIME=81023
# MIND=2094, MA=25, D=4, ENIGMA=390625, PRIME=71023
```
• geoffrounce 28 July 2016 at 9:49 pm

Here is a solution in C# with thanks for help from Brian Gladman.
The loops were worth the effort – it runs in only 11 msec on my laptop!

```using System;
using System.Diagnostics;

namespace Enigma220
{
class Program
{
private static bool isPrime(int num)
{
if (num % 2 == 0)
return num == 2;
int boundary = (int)Math.Floor(Math.Sqrt(num));
for (int i = 3; i <= boundary; i += 2)
if (num % i == 0)
return false;
return true;
}

static void  Main()
{
Stopwatch stopwatch = Stopwatch.StartNew();

int e, n, i, g, m, a, d, p, r;
int enigma, ma, mind, prime;

for (e = 1; e < 10; e++)
{
for (n = 0; n < 10; n++)
{
if (n == e)
continue;
for (i = 0; i < 10; i++)
{
if (i == e || i == n)
continue;
for (g = 0; g < 10; g++)
{
if (g == e || g == n || g == i)
continue;
for (m = 1; m < 10; m++)
{
if (m == e || m == i || m == n || m == g)
continue;
for (a = 0; a < 10; a++)
{
if (a == e || a == i || a == n || a == g || a == m)
continue;
for (d = 1; d < 10; d++)
{
if (d == e || d == i || d == n || d == g || d == m || d == a)
continue;
enigma = 100000 * e + 10000 * n + 1000 * i + 100 * g + 10 * m + a;
ma = 10 * m + a;
if (enigma == Math.Pow(ma, d))
{
for (p = 1; p < 10; p++)
{
if (p == e || p == i || p == n || p == g || p == m || p == a || p == d)
continue;
for (r = 0; r < 10; r++)
{
if (r == e || r == i || r == n || r == g || r == m || r == a || r == d || r == p)
continue;
prime = p * 10000 + r * 1000 + i * 100 + m * 10 + e;
if (isPrime(prime))
{
mind = 1000 * m + 100 * i + 10 * n + d;
Console.WriteLine("ENIGMA={0}, PRIME={1}, MIND={2}", enigma, prime, mind);
stopwatch.Stop();
Console.WriteLine(" Run Time={0} msec", stopwatch.ElapsedMilliseconds);
Console.ReadLine();  // freeze console screen
}
}
}
}
}
}
}
}
}
}
}
}
}
}

// Press <Return> to get 2nd solution

// Output
//-------
//ENIGMA=390625, PRIME=71023, MIND=2094
// Run Time = 11 msec

//ENIGMA = 390625, PRIME = 81023, MIND = 2094
//Run Time = 11 msec

```
• Jim Randell 29 July 2016 at 6:24 pm

It’s always interesting to see solutions in different programming languages. I managed to get it compiled and running on my Raspberry Pi (by installing the mono-gmcs package – I expect it’s somewhat easier on a machine running Microsoft Windows). But it turned out to run about 10× slower than my Python solution, so I’ll probably not be switching over to C#.

• geoffrounce 30 July 2016 at 8:06 pm

Yes,it was easy to compile and run it with Windows 10 and Visual Studio 2015.

One can also run Python under VS 2015 with Python Tools for Visual Studio (PTVS) but I have not explored this add-on in detail. Interesting video on YouTube for PTVS here:

Out of interest I also coded this Enigma in Visual Basic, also in VS 2015. Interesting result was that I found solutions with four different prime numbers, other results being the same. It has same run time as C# version..

```Module Module1

Function isPrime(ByVal num As Integer) As Boolean
If num Mod 2 = 0 Then
Return num = 2
End If

Dim i, boundary As Integer
boundary = Math.Floor(Math.Sqrt(num))

For i = 3 To boundary Step 2
If num Mod i = 0 Then
Return False
End If
Next

Return True
End Function

Sub Main()
' Create new Stopwatch instance.
Dim watch As Stopwatch = Stopwatch.StartNew()

Dim E, N, I, G, M, A, P, D, R As Integer
Dim ENIGMA, PRIME, MA, MIND As Integer

' Get different digits for different letters E, N, I, G, M, A, D, P And R
For E = 1 To 9
For N = 0 To 9
If N = E Then Continue For
For I = 0 To 9
If I = N Or I = E Then Continue For
For G = 0 To 9
If G = I Or G = N Or G = E Then Continue For
For M = 1 To 9
If M = G Or M = I Or M = N Or M = E Then Continue For
For A = 0 To 9
If A = M Or A = G Or A = I Or A = N Or A = E Then Continue For

' Calculate MA and ENIGMA
MA = 10 * M + A
ENIGMA = 100000 * E + 10000 * N + 1000 * I + 100 * G + 10 * M + A
For D = 1 To 9
If D = A Or D = M Or D = G Or D = I Or D = N Or D = E Then _
Continue For

'Check ENIGMA = MA ^ D
If ENIGMA = Math.Pow(MA, D) Then
For P = 1 To 9
If P = A Or P = M Or P = G Or P = I Or P = N Or P = E _
Then Continue For
For R = 0 To 9
If R = D Or R = P Or R = A Or R = M Or R = G Or R = I _
Or R = N Or R = E Then Continue For
PRIME = 10000 * P + 1000 * R + 100 * I + 10 * M + E

'Check PRIME is a prime number
If isPrime(PRIME) Then
MIND = 1000 * M + 100 * I + 10 * N + D
Console.WriteLine("ENIGMA = {0}, PRIME = {1}, MA = {2},_
D = {3}, MIND = {4}", ENIGMA, PRIME, MA, D, MIND)
watch.Stop()
Console.WriteLine(watch.Elapsed.TotalMilliseconds)
Console.ReadLine()  ' freeze console screen
End If
Next R
Next P
End If

Next D
Next A
Next M
Next G
Next I
Next N
Next E

End Sub

End Module
'Output - 4 values for PRIME
'------
'ENIGMA = 390625, PRIME = 41023, MA = 25, D = 4, MIND = 2094
'ENIGMA = 390625, PRIME = 48023, MA = 25, D = 4, MIND = 2094
'ENIGMA = 390625, PRIME = 71023, MA = 25, D = 4, MIND = 2094
'ENIGMA = 390625, PRIME = 81023, MA = 25, D = 4, MIND = 2094
'Run-Time = 11 msec
```
3. Naim Uygun 6 September 2014 at 8:45 pm
```
#Answer: ENIGMA= 390625  MA= 25  D= 4  MIND= 2094
def asal(x):
if x<2: return 0
if x in [2,3,5,7]: return 1
w=int(x**0.5)+2
for q in range(2,w):
if x%q==0: return 0
return 1

from itertools import permutations
for e,n,i,g,m,a,d,p,r in permutations("0123456789",9):
#if '0' in [e,m,d,p]:continue
enigma=e+n+i+g+m+a
ma=m+a
if int(ma)**int(d) != int(enigma): continue
prime=int(p+r+i+m+e)
if asal(prime)==1:
print("ENIGMA=",enigma," MA=",ma, " D=",d," MIND=",m+i+n+d)
#break
```
4. Brian Gladman 9 September 2014 at 3:40 pm
```from itertools import permutations

pr = (2, 3, 5, 7, 11, 13, 17)
pr = pr + tuple(x for x in range(19, 317, 2) if all(x % p for p in pr))

# choose non-zero digits for M and A
for M, A in permutations(range(1, 10), 2):
ma = 10 * M + A
# try D values that can give a 5 digit ENIGMA value
for D in range(3, 6):
enig, r = divmod(ma ** D, 100)
# look for values such that ENIGMA = MA ** D
if r == ma and 1000 < enig < 10000 and D not in (M, A):
E, N, I, G = t = tuple(int(x) for x in str(enig))
mind = 1000 * M + 100 * I + 10 * N + D
# check that digits assigned so far are all different and
# that E is odd (because it is the lowest diigit of PRIME)
rem_ltrs = set(range(10)) - (set(t) | set((M, A, D)))
if E % 2 and len(rem_ltrs) == 3:
# select the digits for P and R from those unused so far
for P, R in permutations(rem_ltrs, 2):
prime = 10000 * P + 1000 * R + 100 * I + 10 * M + E
# check that PRIME is prime
if all(prime % p for p in pr):
fs = 'MIND = {}, PRIME = {}, ENIGMA = {} = {}^{}'
print(fs.format(mind, prime, 100 * enig + ma, ma, D))
```