Take away $$\overline{MAD}$$ from $$\overline{MADMADAM}$$ to make her prime. $$\overline{ADAM}$$ is prime too if he does not get mad: $$\overline{MADADAM}.$$

$$M, A, D$$ are 3 distinct digits that are to be determined by you so that the statements in this puzzle are true.

• I don't know why but this puzzle infuriates me! (nice puzzle +1) – Adam May 4 '19 at 21:06
• @Adam computers fix everything :D – Brandon_J May 4 '19 at 21:14
• I understand Adam..nothing personal ..I couldn’t resist the palindrome rhyme to be included in the problem.. – Uvc May 4 '19 at 21:16
• From the statement "ADAM" is prime too if he does not get mad: "MADADAM", can we conclude that ADAM is a four-digit number, hence A != 0? Thus constraining A to 1..9? – smci May 6 '19 at 11:16
• Also, from "ADAM" is prime too if he does not get mad, are you saying MADADAM must be composite? or that's unspecified? If MADADAM must be composite, we can apply the simple divisiblity-by-n congruences for n=3,7,11 etc. to prune candidates. – smci May 7 '19 at 2:28

So, we have two prime numbers (MADMADAM-MAD, ADAM).

Consequently, from a more mathematical standpoint,

$$M*10,010,001 + A* 1,001,010 + D*100,100 - (M*100 + A*10 + D)$$ is prime, as is $$A*1,010 + D*100 + M$$.

I ran this through a little program that I wrote, and I discovered that there are three distinct solution sets. They are:

1. M: 1 A: 5 D: 8 --> $$15,815,851 - 158 = 15,815,693‬$$, which is prime. $$5851$$ is also prime.

2. M: 3 A: 1 D: 6 --> $$31,631,613 - 316 = 31,631,297‬$$, which is prime. $$1613$$ is also prime.

3. M: 3 A: 5 D: 6 --> $$35,635,653 - 356 = 35,635,297$$, which is prime. $$5653$$ is also prime.

If you're interested, here's the Java program that I wrote to find the answer. It probably isn't super efficient, but it works.

public class Test {

public static void main(String[] args) {
int big = 0;
for(int i = 1; i<=9; i++) {
for(int j = 1; j<=9; j++) {
for(int k = 0; k<=9; k++) { //k (or d) is the only digit which could be 0.
if(isPrime(i*10010001 + j* 1001010 + k*100100 - (i*100 + j*10 + k))&&
isPrime(j*1010 + k*100 + i)) {
big = i*10010001 + j* 1001010 + k*100100 - (i*100 + j*10 + k);
System.out.println("M: " + i + " A: " + j + " D: " + k);
System.out.println(""+i+j+k+i+j+k+j+i+"-"+ i+j+k+"="+big+
", which is prime. "+j+k+j+i+" is also prime.\n");
}
}
}
}
}
public static boolean isPrime(int i) {
boolean prime = true;
for(int j = 2; j<=i/2; j++) {
if (i%j==0) {
prime = false;
break;
}
}
return prime;
}
} 

• Nice, but can you try a little more constraining and less brute-force in the solution? M has to be one of 1,3,7,9. D has to be even since MADMADAM-MAD is also prime and cannot end in 5. Hence (D-M) has to be congruent to one of 2,4,6,8 mod 10. A is then constrained by the D,M values such that ADAM is not divisible by either 3,9 or 11. – smci May 5 '19 at 0:22
• @smci perhaps you could create an answer with that logic? As it stands, the OP has not disallowed brute force, so my post fully answers the question. The logic that you mentioned would be a bit "extra" in my solution, but I'm fairly certain that you could craft a new answer where the logic fits in perfectly. My original algorithm didn't contain the modifications you mentioned largely because (just being honest) it was most straightforward to program it with minimal constraints. – Brandon_J May 5 '19 at 0:43
• I think algebraic solution is preferable (e.g. the 'MADMAD' part is clearly a multiple of 1001 = 7×11×13), but constrained-heuristic is okay. If anyone can give me more hints on constraints, I would code it up. Maybe if we develop the constraints enough, algebraic and constrained-heuristic will sort of meet-in-the-middle... – smci May 5 '19 at 2:35
• Yo, I posted my heuristic code that only needs to test 160 candidates for ADAM, of which only 52 prime candidates need to get the final check for the eight-digit (MADMADAM-MAD) also being prime. I think it's quite beautiful ;-) But perhaps you or others can suggest further improvements. – smci May 6 '19 at 12:18
• I am not a programmer currently. Your logic is good. When I created it, started from the phrase and known 1001 properties, came up with 193 91 3 193 91. I knew it won’t be unique. – Uvc May 8 '19 at 12:33

Here's a better heuristic that only needs to test 52 candidates for ADAM, not all 10×9×8=720 candidates for M,D,A. Brute-forcing is an ugly sledgehammer, it's not scaleable. We can't get a pure algebraic solution, but let's try to refine things into an efficient constrained-search:

• The prime 'ADAM' ends in M, hence M has to be one of [1,3,7,9]
• D has to be even [0,2,4,6,8] since (MADMADAM-MAD) is also prime and thus cannot end in 5. (In fact you can get a better constraint due to its last digit (M-D) needing to be congruent to one of 2,4,6,8 mod 10, cannot be 0, but let's ignore that 20% improvement).
• Last, A could be nearly anything: odd or even (and might even be zero, although the hint ADAM is prime is ambiguous about A==0). But also M,D,A must all be distinct, so there are only 8 choices for A (given M,D) instead of 10:
• we only have to test at most 4×5×10 = 200 candidates for M,D,A
• in fact only 4×5×8 = 160, due to the M,D,A-all-distinct constraint
• But we were told ADAM is prime, which prunes it to only 52 ADAM candidates to check further...! Prime-testing 160 four-digit numbers is cheap.
• (Ballpark estimation would have estimated since there are 1061 four-digit primes, D being even reduces those by 2x, and the 'ADAM' pattern locks the value of the third digit to the first, should further reduce them by ~10x, hence ~20x overall giving us 53. Actually turns out only 52 candidates.)
• Last, we postpone the far most computationally expensive check if (MADMADAM-MAD) is prime, to only our 52 candidates for which ADAM is prime. I'm sure someone can find a better way than brute-force-testing 52 eight-digit numbers (e.g. we can prune a little with the well-known divisibility-by-3,7,11 tests).
• (Update: Brandon_J: pointed out we all missed that MADADAM apparently must also be checked to be composite. That's just a trivial extra one-liner.)

Here's my Python 3 code:

from sympy import isprime

for m in [1,3,7,9]:
for d in [0,2,4,6,8]:
for a in range(10): # TODO refine constraints on a

# Check all digits distinct
if a==d or a==m: continue

# Check ADAM must be prime
if not isprime(1010*a + 100*d +m): continue
#print(f'Checking A={a}, D={d}, M={m} | ADAM={a}{d}{a}{m} is prime')


Can anyone suggest any further improvements on constraints or algebraically? In the for-loop that picks A, we could further constrain the choice of A (by congruences) such that it will not later fail either the divisibility-by-3 or -11 tests on either ADAM or (MADMADAM-MAD). Ballpark we might expect that to reduce to (2/3) * (10/11) ≈ 60% of candidates.
• @Brandon_J: oh, I totally missed that: that MADADAM must also be checked to be composite. Sure, that's a trivial extra one-liner. But I'm really more interested in refining the constrained-search, to reduce the number of candidates as early, aggressively and simply as possible, before we get to primality-testing. – smci May 6 '19 at 16:56