# Is there any perfect square in the sequence $12,123,1234,12345,...$?

Let $x_n$ be the number constructed by concatenating the first $n$ positive integers. Is there any perfect square in the sequence $(x_n)_{n ≥ 2}$ ?

This is OEIS A007908. This is the sequence of Smarandache numbers. We don't know any index $n$ such that $x_n$ is a prime number (see here, or OEIS page linked before).

This Mathematica code suggests that there is no perfect square for $n<1000$:

For[i = 1; t = i, i < 1000,
t = 10^(Floor[Log10[i]] + 1)*t + i, ++i;
If[ Floor[Sqrt[t]] == t, Print[t]]
]


Similarly, using Surd[t, k] it seems that $x_n$ is not a $k$-th power for $n≤1000$, $k≤5$. According to On the back concatenated square sequence (Ling Li), the similar sequence $14,149,14916,1491625$ obtained by concatenating the first $n$ squares is conjectured to contain no perfect square. I think that induction could be used.

• squares are scarcer than primes Commented Aug 24, 2016 at 21:22
• Here's something: we have $x_n\equiv n(n+1)/2\bmod 9$. The quadratic residues mod 9 are 0,1,4,7, and the values of $n(n+1)$ mod 9 are 0,2,3,6. So if there is a square, it occurs when $n\equiv \pm 1\bmod 9$. I think you can do something similar with $\bmod 11$, and this should at the very least make it easier to search larger numbers. Commented Aug 24, 2016 at 21:28
• You can calculate them non-recursively if you want. And this will decrease how many numbers you need to check. Commented Aug 24, 2016 at 21:35
• calculating non recursively is a terrible idea, you have to calculate the whole number each time you check. Commented Aug 24, 2016 at 21:47
• Similar , it is very unlikely that we can form a palindrome with the first $n$ digits of $e$ after the deimal point for any integer $n>1$ , we have infinite many chances , but the chance decreases so fast that from some point on it is virtually impossible to reach a palindrome. Commented Jul 7, 2023 at 19:29

The last digit of any perfect square must be 0, 1, 4, 5, 6, 9. So we must have $$n \in \{ 0, 1, 4, 5, 6, 9 \}$$ (mod 10).

Recall that the sum of the first $$n$$ integers is $$\frac{n(n+1)}{2}$$, and every integer is congruent modulo 9 to the sum of its digits. Reading down the diagonal of the base-9 multiplication table, we see that perfect squares end in 0, 1, 4, or 7. So we must have $$\frac{n(n+1)}{2} \in \{ 0, 1, 4, 7 \}$$ (mod 9). Equivalently, $$n \in \{0, 1, 4, 7, 8\}$$ (mod 9).

Combining the mod 10 and mod 9 results, we get:

$$n \in \{0, 1, 4, 9, 10, 16, 19, 25, 26, 31, 34, 35, 36, 40, 44, 45, 46, 49, 54, 55, 61, 64, 70, 71, 76, 79, 80, 81, 85, 89 \}~ (\text{mod } 90)$$

Now, let's consider the last two digits of the number. All perfect squares end in 00, 01, 04, 09, 16, 21, 24, 25, 29, 36, 41, 44, 49, 56, 61, 64, 69, 76, 81, 84, 89, or 96. Of these, the only ones containing consecutive digits are 01, 56, and 89. But since $$n = 1$$ is explicitly ruled out by the question, and $$n = 6$$ ($$\sqrt{123456} \approx 351.36306$$) and $$n = 9$$ ($$\sqrt{123456789} \approx 11111.111061$$) fail to produce perfect squares, then the last two digits must be part of $$n$$ itself.

Combining the mod 90 and mod 100 results, we get:

$$n \in \{0, 1, 4, 9, 16, 25, 36, 44, 49, 61, 64, 76, 81, 89, 100, 109, 116, 121, 124, 125, 136, 144, 161, 169, 181, 184, 189, 196, 216, 224, 225, 229, 241, 244, 256, 261, 269, 289, 296, 301, 304, 316, 324, 325, 341, 349, 361, 364, 369, 376, 396, 400, 404, 409, 421, 424, 436, 441, 449, 469, 476, 481, 484, 496, 504, 521, 529, 541, 544, 549, 556, 576, 584, 589, 601, 604, 616, 621, 625, 629, 649, 656, 661, 664, 676, 684, 700, 701, 709, 721, 724, 729, 736, 756, 764, 769, 781, 784, 796, 800, 801, 809, 829, 836, 841, 844, 856, 864, 881, 889\}~ (\text{mod } 900)$$

This doesn't solve the problem, but it would reduce the effort of a brute-force search, since you only have to try 110 of every 900 consecutive integers. I'll see if there's a way to filter the list even further.

If $$n$$ is a square, then the next square is $$(\sqrt{n}+1)^2\approx n+ 2\sqrt{n}$$, so the density of squares is around $$2/\sqrt{n}$$.

$$n$$ has around $$\log(n)$$ digits, so $$x_n$$ has around $$n \log(n)$$ digits, so $$x_n \approx 10^{n \log(n)}\approx n^n$$.

Thus using a density heuristic, $$x_n$$ is a square with probability $$2 n^{-n/2}$$ which decreases very fast, so the expected number of square $$x_n$$ after $$N$$ is around $$\sum_{n\geq N} 2n^{-n/2} \approx 2N^{-N/2}$$.

Since you’ve already checked the first $$1000$$ numbers, the chance that any later number in the sequence is a square is at most $$2\times 10^{-1500}\approx 0$$. Thus, according to this heuristic, it’s vanishingly unlikely that there are any squares in this sequence.

Note that this isn’t exactly correct since $$x_n$$ isn’t equidistributed mod 9, but I don’t think the core point changes much even if you handled that more carefully.

• Nice approach to the problem. Commented Jul 6, 2023 at 15:32

Following Samir Khan's comment, using $$2 \times 5 \equiv 1 \pmod 9$$, by enumerating all possible values:

$$x_n \equiv \frac{1}{2} n (n + 1) \equiv 5 n (n + 1) \in \{0,1,3,6\} \pmod 9$$ and $$m^2 \in \{0,1,4,7\} \pmod 9$$

Therefore,

$$\exists m\in\mathbb{N} . m^2 = x_n \implies x_n \in \{0,1,3,6\} \cap \{0,1,4,7\} \equiv \{0,1\} \pmod 9$$

Looking at the values taken by $$5 n (n+1) \pmod 9$$, we see that this is satisfied by $$n \in \{ 0,1,4,7,8 \} \pmod 9$$

Consider the final digits in base $$10$$, as in Dan's answer. We have that for $$n$$ with $$m$$ digits (with $$n$$ not equal to $$10^{m-1}$$), then $$n$$ must be a quadratic residue modulo $$10^m$$ and $$(n-1) 10^m + n$$ must be a quadratic residue modulo $$10^{2m}$$. In general, for $$10^{m-1} + k -1 \le n < 10^m$$, then $$\sum_{j=0}^{k-1} (n - j) 10^{jm}$$ must be a quadratic residue modulo $$10^{km}$$.

Combining with the modulo $$9$$ test, enumerating the possible $$n$$ by these criteria one finds by a computer search (for example, using an efficient quadratic residue test) that the proportion of candidate $$n$$ seems to stabilize at about $$3.5\%$$, even as $$m$$ and $$k$$ are increased, which I find surprising.

Meanwhile, from the other end, the leading digits of the square root of $$x_n$$ (for example, using a Vedic algorithm) are one of

$$11111111065105601373995021196\ldots$$ or $$35136418300833130224757756198\ldots$$ depending on whether the number of digits of $$x_n$$ is odd or even.

The calculation can be formulated as algorithms (similarly to Yuri Negometyanov's answer) that emit digits of the square roots: if either terminates, then an $$x_n$$ is a perfect square. Proving (non)termination may not be any easier than the original problem.

Here is a simplified example implementation in Haskell (this program may give a false positive due to truncating the concatenated digit sequence in the middle of one of the appended numbers...):

import Data.List (elemIndex)
import Data.Maybe (fromJust)
import Numeric (showIntAtBase)

b :: Integer
b = 10

digits :: [Char]
digits = "0123456789"

string :: [Integer]
string = map (fromIntegral . fromJust . (elemIndex digits)) .
concat . map (\n -> showIntAtBase (fromIntegral b) (digits !!) n "") $[1..] isqrt :: Integer -> Integer -> [Integer] -> [Char] isqrt acc s (x:y:zs) = (digits !! fromIntegral d) : if r == 0 && acc /= 0 {- && not an incomplete concatenation -} then [] else isqrt (b * acc + 2 * d) (b^2 * r + b * x + y) zs where d = maximum [ d | d <- [0..b-1], (b * acc + d) * d <= s ] r = s - (b * acc + d) * d main' :: [Integer] -> ([Char], [Char]) main' (x:y:zs) = (isqrt 0 x (y:zs), isqrt 0 (b * x + y) zs) main :: IO () main = do let (x, y) = main' string mapM_ print (zip3 [1..] x y)  Calculation hasn't terminated so far, currently at about $$4000000$$ digits of output after about 3.5 hours of computation. • Nice answer! I've checked in Mathematica that$\sum_{j=0}^{k-1}(n-j)10^{jm}=\frac{\left(10^{k m}-1\right) \left(10^m (n+1)-n\right)-k \left(10^m-1\right) 10^{k m}}{\left(10^m-1\right)^2}$, which modulo$10^{km}$becomes$\frac{n-10^m (n+1)}{\left(10^m-1\right)^2}$. Since the denominator of the previous expression is a quadratic residue, it all simplifies to checking that$n-10^m (n+1)$is a quadratic residue modulo$10^{km}$for every$k$, which is a very strong set of conditions. For example, between$1$and$100$only$n\in\{1,6,41,49,81,89,100\}\$ satisfies these conditions. Commented Jul 8, 2023 at 10:24

Denote $$\dbinom q{r}_d = \dbinom {q-rd}{r+d}=\dbinom{q_{next}}{r_{next}}.$$ Then digits of the square root can be calculated similarly the algorithm of dividing by corner, wherein $$q=0$$ is a feature of the perfect square, with the square root value $$\frac r2$$.

If the perfect square has odd quantity of digits, then the algoritm can be presented in the form of \begin{align} &\dbinom{\mathbf1}{1}_1=\dbinom02,\quad\;\dbinom{0\mathbf{23}}{21}_1=\dbinom{2}{22}, \quad\dbinom{2\mathbf{45}}{221}_1=\dbinom{24}{222}, \quad\dbinom{24\mathbf{67}}{222\mathbf1}_1=\dbinom{246}{2222},\\[4pt] &\dbinom{246\mathbf{89}}{22221}_1=\dbinom{2468}{22222}, \quad\dbinom{2\,468\mathbf{10}}{2\,22221}_1=\dbinom{24589}{2\,22222}, \quad\dbinom{24589\mathbf{11}}{22\,22221}_1=\dbinom{2\,36690}{22\,22222},\\[4pt] &\dbinom{236\,690\mathbf{12}}{222\,22221}_1=\dbinom{14\,46791}{222\,22222}, \quad\dbinom{1446\,791\mathbf{13}}{2222\,22220}_0=\dbinom{1446\,79113}{2222\,22220},\\[4pt] &\dbinom{1\,44679\,113\mathbf{14}}{22222\,22206}_6=\dbinom{11345\,78078}{22222\,22212}, \quad\dbinom{11\,34578\,078\mathbf{15}}{2\,22222\,22125}_5=\dbinom{23466\,97190}{2\,22222\,22130},\dots \end{align}

If the perfect square has even quantity of digits, then the algoritm can be presented in the form of \begin{align} &\dbinom{\mathbf{12}}{3}_3=\dbinom36,\quad\;\dbinom{3\mathbf{34}}{65}_5=\dbinom{9}{70}, \quad\dbinom{9\mathbf{56}}{701}_1=\dbinom{255}{702}, \quad\dbinom{255\mathbf{78}}{702\mathbf3}_3=\dbinom{4509}{7026},\\[4pt] &\dbinom{4509\mathbf{91}}{70266}_6=\dbinom{29395}{70272},\dots \end{align}

Described algorithm allows to test all number sequence in the two calculating flows. If the intermediate result does not correspond with the given sequence, it can be skipped.

$$\textbf{Appendium}$$

There is an algorithm which I have got at the school. Translated from my post at the other site. It allows to extract a square root by a column, like dividing by a corner.

The algorithm is based on the identity $$\mathbf{(ae+b)^2=a^2\,e^2 + (2ae+b)b}$$ for $$\mathbf{e=10}$$.

The role of a is the value of the square root calculated for the highest digits of the number, the role of b is the new digit. The root value is calculated by deficiency, the number of decimal digits of the result is not limited.

Unlike the column division algorithm, two new digits of the original number are involved in obtaining a new digit at once, and when receiving a fractional part, zeros are also added in pairs. This is what the multiplier $$\mathbf{e^2=100}$$ in the formula tells us about.

So that the result does not get the factor $$\mathbf{\sqrt{10}\approx 3.162}$$, the numbers are grouped in pairs, starting from the decimal point. In this case, the highet significant group contains one or two digits.

The figure shows the process of extracting the square root of the number $$\mathbf{N = 69696}.$$

The number N is placed in the usual way. Instead of a corner on the right, we wrote the sign $$\mathbf{=}.$$ And almost immediately they wrote the first digit $$\mathbf{b_2=a_2=2},$$ because $$\mathbf{\lfloor\sqrt6\rfloor=2}.$$ It was in the hundreds.

At the second iteration, we argue as follows: the left side of the identity is on top, the number $$\mathbf{a_2=2}$$ is on the right of the equal sign. Subtracting the value $$\mathbf{a_2^2=4}$$ from the top number and moving down a couple of digits $$\mathbf{96}$$ to this deuce, we get the equality $$\mathbf{(2a_2e+b_1)b_1\le296},$$ where $$\mathbf{b_1}$$ is the unknown digit in the digit tens. We draw horizontal and vertical lines, as in the figure, and to the left of the vertical line, indented to the left by one digit, write the number $$\mathbf{2a_2=4}.$$

If we concatenate to this the number $$\mathbf{b_1}$$ to the right side, we get just $$\mathbf{2a_2e+b_1}.$$ And if the same number $$\mathbf{b_1}$$ is signed below, then we get an example for multiplication with the answer 296. Let's pick this number in our mind: $$\mathbf{45x5=225}-$$ not enough, $$\mathbf{47x7=329}$$ - already too much, $$\mathbf{46\times6=276}$$ - just right! And immediately write down $$\mathbf{276}$$ under $$\mathbf{296}$$ - so as not to forget. And we add the righteously obtained number $$\mathbf{b_1=6}$$ to the answer on the right.

So, the third iteration. And something is already clear: two new dashes, subtraction by a column and demolition of the next two digits - $$\mathbf{2096}\dots$$ And here is the nuance: $$\mathbf{(2a_2e+b_1)+b_1\ge2(a_2e+b_1)=2a_1}.$$ Those. we do not have to multiply in our minds $$\mathbf{26\times2},$$ but we can add $$\mathbf{46}$$ and $$\mathbf{6}$$ in a column. And get $$\mathbf{52}.$$

So, $$\mathbf{52b_0\times b_0 = 2096\dots... b_0=4}$$ - hooray! Carefully paint on $$\mathbf{4}$$ in multiplication, $$\mathbf{2096}$$ on the bottom, $$\mathbf{4}$$ on the right. The root was extracted safely completely.

Now nothing prevents to formalize the algorithm, and for any number system.

The question may arise: why this algorithm, if there is an iterative algorithm of Heron of Alexandria? Just because it has pluses - the same as dividing by a corner:

1. The ability to calculate the result at a speed of ~10 operations (comparison, subtraction, logical OR and shifts) per bit of the result with a hardware error.
2. Ease of use for long binary numbers represented by arrays.
3. Ability to extract the square root manually in any number system.
4. Guaranteed presentation of the result by deficiency.

EDIT: The tables were completely wrong.

Following Samir Khan's advice, let's consider modulo 11:

Let $$d_n$$ denote the number of digits of $$n$$. It can be shown that if $$d_n$$ is even, then $$x_n\equiv 6(1+n+n^2)-d_n\pmod{11}.$$ It took me a while to show this, maybe some of you can find a simple proof; I'll write mine at the end of this post. On the other hand, if $$d_n$$ is odd and $$n$$ is even we have $$x_n\equiv -(1+5n)+d_n\pmod{11}.$$

Finally, if $$d_n$$ and $$n$$ are both odd we have $$x_n\equiv -(4+5n)-d_n\pmod{11}.$$

In short, modulo $$11$$ doesn't tell us much. Maybe you can get similar congruences modulo $$12$$ that would be more helpful, since $$12$$ doesn't have as much quadratic residues as $$11$$.

## PROOF OF THE CONGRUENCES

Denote the number of digits of $$x_n$$ by $$D_n$$; to simplify notation we'll write $$D=D_n$$ and $$d=d_n$$. Consider $$x_n$$'s base $$10$$ expansion: $$\begin{array}{} x_n&=&10^{D-1} 1&+&10^{D-2}2&+&10^{D-3} 3&+&\cdots&+&10^{D-9} 9\\ &&10^{D-11}10&+&10^{D-13}11&+&10^{D-15}12&+&\cdots&+&10^{D-189}99\\ &&10^{D-192}100&+&10^{D-195}101&+&10^{D-198}102&+&\cdots&+&10^{D-2889}999\\ &&\cdots\\ &&\cdots&+&10^{D-D}n \end{array}$$ Notice that the exact value of the $$10$$'s exponents doesn't matter: it only matters if they are odd or even. Modulo $$11$$ we obtain: $$\begin{array}{} x_n&\equiv&(-1)^{D-1} 1&+&(-1)^D2&+&(-1)^{D-1} 3&+&\cdots&+&(-1)^{D-1} 9\\ &&(-1)^{D-1}(\quad10&+&11&+&12&+&\cdots&+&99\quad)\\ &&(-1)^{D}100&+&(-1)^{D-1}101&+&(-1)^{D}102&+&\cdots&+&(-1)^{D-1}999\\ &&\cdots\\ &&\cdots&+&n \end{array}$$ Observe the pattern: we are getting alternating sums of the form $$s_1(k):=\sum_{i=10^k}^{10^{k+1}-1}(-1)^{D-i}i$$ in the odd rows and easy sums $$s_2(k):=(-1)^{D-1}\sum_{i=10^k}^{10^{k+1}-1}i$$ in the even rows. Moreover, we get a final sum from $$10^{d-1}$$ up to $$n$$ which can have either of the two previous forms.

Let's consider first the easy sums $$s_2(k)$$. Using the school formula for the sum of consecutive numbers we obtain $$s_2(k)=\frac{(-1)^{D-1}}{2}(10^{k+1}-10^k)(10^{k+1}+10^k-1).$$ Reduce modulo $$11$$ to get ($$k$$ is odd since we sum the even rows) $$s_2(k)\equiv (-1)^{D}\pmod{11}.$$ Now, let's examine the sum of $$s_2(k)$$ for the even rows, that is, for $$k$$ ranging between $$1$$ and the last even row's $$10$$ power. If I'm not mistaken, this should be $$S_2:=\sum_{\substack{k=1\\k+=2}}^{d-\frac{(-1)^d+5}{2}}s_2(k)\equiv(-1)^D\bigg(1+\Big\lfloor\frac{1}{2}\Big(d-\frac{(-1)^d+5}{2}-1\Big)\Big\rfloor\bigg)\equiv(-1)^D\bigg(1+\Big\lfloor\frac{1}{4}(2d-7-(-1)^d)\Big\rfloor \bigg)\pmod{11}.$$ Similarly, it can be shown that for $$k>0$$ $$s_1(k)\equiv (-1)^{D}\pmod{11},$$ and $$S_1:=\sum_{\substack{k=2\\k+=2}}^{d+\frac{(-1)^d-5}{2}}s_1(k)\equiv(-1)^{D} \bigg(1+\Big\lfloor \frac{1}{4}(2 d-9+(-1)^d)\Big\rfloor \bigg)\pmod{11}.$$ Aditionally, the floor expressions appearing in $$S_1$$ and $$S_2$$ can be combined to get $$S_1+S_2\equiv (-1)^D(d-2)\pmod{11}.$$ Next, compute the sum of the first row (which we skipped because we assumed $$k>0$$): $$\sum_{i=1}^9(-1)^{D-i}i=-5(-1)^D.$$ Therefore, $$\text{Sum of all the rows minus the last one}\equiv S_1+S_2-5(-1)^D\equiv(-1)^D(d+4)\pmod{11}.$$ Notice this shouldn't work for $$n<10$$, since we would be summing the whole first row. Moreover, Wolfram MathWorld page on Smarandache numbers says that $$D=(n+1)d-\dfrac{1}{9}(10^d-1)$$. So, simplifying, $$\text{Sum of all the rows minus the last one}\equiv -(-1)^{(n+1)d}(d+4)\pmod{11}.$$ Thus, it all boils down to consider the different possibilities for the last row. For example, if $$d$$ is even, the last row becomes $$(-1)^{D-1}\sum_{i=10^{d-1}}^n i=\frac{(-1)^{D-1}}{2}(n-10^{d-1}+1)(n+10^{d-1})\equiv 6(-1)^{D-1}(n+2)(n-1)\pmod{11}.$$ Employing again the equality for $$D$$ we get $$\text{Sum of last row}\equiv 6(n+2)(n-1)\pmod{11},$$ and so $$\text{Sum of all rows}\equiv -(-1)^{(n+1)d}(d+4)+6(n+2)(n-1)\equiv 6(1+n+n^2)-d\pmod{11}.$$ The other cases are analogous.