# Solve equation with summation and factorial over Integers

Find all $$\{ m, n \} \in \mathbb{Z}^+$$ such that

$$\sum\limits_{k=1}^m k! = n^2 .$$

The two solutions are $$\{ m,n \} = \{ 1,1 \}$$ and $$\{ 3,3 \}$$, as can be confirmed. Here is how to solve this problem mathematically. I seek to solve it using computer symbol manipulation (and preferably not through exhaustive search.)

The obvious approaches do not work:

Solve[Sum[k!, {k,1,m}] == n^2 && {m, n} > 0, {m, n}, Integers]


and

FindInstance[Sum[k!, {k,1,m}] == n^2 && {m, n} > 0, {m, n}, Integers]


Any suggestions?

• Ask it at MSE. This is rather math than Mathematica. Aug 6, 2021 at 18:55
• I know how to solve it mathematically. (Michael Penn solved it on his Youtube channel.) I want to know how to do it in Mathematica. Aug 6, 2021 at 19:00
• In each of your Sums, the summand should be k! vice n!. However, that does not resolve the issue. A brute force approach: Select[Flatten[Table[{m, n}, {m, 1, 100}, {n, 1, 100}], 1], Sum[k!, {k, 1, #[[1]]}] == #[[2]]^2 &] Aug 6, 2021 at 19:03
• The command FindInstance[Sum[k!, {k, 1, m}] == n^2 && {m, n} > 0, {m, n}] results in {{m -> 1, n -> 1}}. Aug 7, 2021 at 7:38
• Solve, Reduce. Minimize, and FindMinimum all promptly convert Sum[k!, {k,1,m}] to -1 - Subfactorial[-1] + (-1)^(1 + m) Gamma[2 + m] Subfactorial[-2 - m], which apparently is correct. However, if the Reals or Integers domains are specified, these functions eventually realize that Subfactorial[-1] is complex and quit with an error message. I view this as a bug, because this internal error should not leak out. In fact, it occurs even for the trivial SolveValues[Sum[k!, {k, 1, m}] == n, n, Reals]. Aug 7, 2021 at 13:26

Check the QuadraticResidues resource function https://resources.wolframcloud.com/FunctionRepository/resources/QuadraticResidues/

QuadraticResidues[5]
(* {0, 1, 4} *)


Thus, 3 is the quadratic nonresidue (mod 5).

Next, observe that this comes at contradiction with the property of the LHS for $$m>4$$:

f[m_] := Sum[k!, {k, 1, m}]
Table[Mod[f[m], 5], {m, 15}]
(* {1, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3} *)


This fact is easy to see from $$n!=0\,(\text{mod}\, 5)$$ for $$n\ge5$$.

Thus, the only possibility $$m=1$$ and $$m=3$$. Now solve the equation

Solve[f[1] == n^2 && n > 0, n, Integers]
Solve[f[3] == n^2 && n > 0, n, Integers]


Notice that for completely automatic solution it is necessary that MA "knows" quadratic residues in some simple cases. Currently besides the resource function, this in not available.

• Wow... I never would have come across that clever approach on my own. Thanks so much. ($+1$). Actually, because this is the most symbolic approach: $\checkmark$. Aug 8, 2021 at 16:20
• @DavidG.Stork Thank you! Really appreciate. Aug 8, 2021 at 16:47

Numerical solution with NMinimize / NMaximize  .

su[m_?NumericQ] := NSum[k!, {k, 1, m}, NSumTerms -> m]

nmin = NMinimize[{n, {m >= 1, m \[Element] Integers, n >= 1,
n \[Element] Integers, (su[m] - n^2) == 0}}, {m, n}]

(*   {1., {m -> 1, n -> 1}}   *)

nmax = NMaximize[{n, {m >= 1, m \[Element] Integers, n >= 1,
n \[Element] Integers, (su[m] - n^2) == 0}}, {m, n}]

(*   {3., {m -> 3, n -> 3}}   *)

• Interesting approach... I certainly hadn't thought of that. Thanks. ($+1$) Aug 8, 2021 at 16:18