# An alphametic for Nikolai Gogol

"Diary of a Madman" (Russian: Записки сумасшедшего, Zapiski sumasshedshevo) is a short story by Nikolai Gogol.

    MAD * MAN = ASYLUM


Which digit does each letter represent? (Please present the full analysis how these digits can be determined. Every letter stands for a digit in base-10 representation, different letters stand for different digits, and leading digits are always non-zero.)

The solution is

$M = 6, A = 4, S = 1, Y = 9, L = 2, U = 5$, with $D, N$ taking either value in $\{7, 8\}$.

Analysis.

Since both factors are very close in value, we can place a lower bound on $M$ with the square root of the smallest 6-digit valid number. $\sqrt[]{123456} \approx 351.36$ results to $M \ge 3$.

From $D * N = M$ we know all 3 numbers must be different. This means that neither $D$ nor $N$ can be in $\{0, 1, 5\}$. This is because $0 * x = 0$, $1 * x = x$, $5 * odd = 5$ and $5 * even = 0$. By extension, $M \ne 5$. We also have $M \ne 9$, because that would require an illegal product from $\{1*9, 3*3, 7*7\}$.

We can enumerate all valid $(D, N)$ pairs. To simplify our work, we assume $D \gt N$ and for any solution found we have a pair of solutions where we can switch them around. As mentioned above, we also require $M = D * N \pmod{10} \ge 3$. And since we require all three letters being different, if one letter is $6$, the other can't be $2$, $4$ or $8$.

By brute forcing the pairs we can determine $M$. Then, the values for $ASYLUM$ can range anything from $M1D * M1N$ to $M9D * M9N$, which restricts the values $A$ can take. We can simplify our calculations by approximating $M00^2 \lt ASYLUM \lt X00^2$, where $X = M + 1$. For example, for $M = 6$, $360000 \lt ASYLUM \lt 490000$, so $A \in \{3, 4\}$. However, we also note that the smallest product for $A = 3$ is $632 * 634 \gt 400000$, which creates the contradiction that $A = 3$ in the factors and $A = 4$ in the result. With this reasoning we can narrow down $A$ to a single value for each value of $M$.

- $M = 3; A \in \{0, 1\} \Rightarrow A = 1$
- $M = 4; A \in \{1, 2\} \Rightarrow A = 1$
- $M = 6; A \in \{3, 4\} \Rightarrow A = 4$
- $M = 7; A \in \{4, 5, 6\} \Rightarrow A = 5$
- $M = 8; A \in \{6, 7, 8\} \Rightarrow A = 7$

D = 2
- N = 3; M = 6, A = 4, ASYLUM = 412806, Y = 2 = D. Contradiction.
- N = 4; M = 8, A = 7, ASYLUM = 762128, Y = 2 = D. Contradiction.
- N = 7; M = 4, A = 1, ASYLUM = 171804, Y = 1 = A. Contradiction.
- N = 8; M = 6, A = 4, ASYLUM = 416016, Y = 6 = M. Contradiction.
- N = 9; M = 8, A = 7, ASYLUM = 766488, S = 6 = Y. Contradiction.

D = 3
- N = 6; M = 8, A = 7, ASYLUM = 764748, S = 6 = N. Contradiction.
- N = 8; M = 4, A = 1, ASYLUM = 172634, U = 3 = D. Contradiction.
- N = 9; M = 7, A = 5, ASYLUM = 571527, S = 5 = M. Contradiction.

D = 4
- N = 7; M = 8, A = 7 = N. Contradiction.
- N = 9; M = 6, A = 4 = D. Contradiction.

D = 6
- N = 9; M = 4, A = 1, ASYLUM = 174304, Y = 4 = M. Contradiction.

D = 7
- N = 8; M = 6, A = 4, ASYLUM = 419256. Solution.
- N = 9; M = 3, A = 1, ASYLUM = 101123, Y = 1 = A. Contradiction.