3

I'm working on a Koshy's elementary number theory exercise and need help finding all triangular numbers less than 1000 that are palindromic.

The problem says as follows: Find all triangular numbers $t_n = \frac{n(n+1)}{2}$ where $t_n < 1000$ and $t_n$ reads the same forwards and backwards (palindromic).

I started computing triangular numbers systematically:

  • $t_1 = 1$ (palindromic ✓)
  • $t_2 = 3$ (palindromic ✓)
  • $t_3 = 6$ (palindromic ✓)
  • $t_{10} = 55$ (palindromic ✓)

Is there a more efficient method than brute force computation to find palindromic triangular numbers?

Any insights would be greatly appreciated!

Bill Dubuque
  • 285,713
John Doe
  • 89
  • 4
  • 9
    There aren't that many triangular numbers $<1000$...I'd think that a direct search would be a lot faster than anything else. – lulu Sep 22 '25 at 00:07
  • 2
    Just as a point of interest, the sequence of palindromic triangular numbers is A003098 – lulu Sep 22 '25 at 00:15
  • @lulu I started with that approach, but I found it rather overwhelming. Your link is quite useful, though. So far, I notice that a palindromic number can it be represented as the sum of previous triangular palindromic numbers (their subindex); I mean, $t_{36}=t_{34}+t_2$. What do you think? – John Doe Sep 22 '25 at 00:24
  • 2
    I don't think any pattern of that form is obvious...more likely to be a coincidence of small numbers. But, really, there are only $44$ triangular numbers less than $1000$ (there are about $\sqrt {2N}$ triangular numbers less than $N$ and $\sqrt {2000}=44.721$). – lulu Sep 22 '25 at 00:30

4 Answers4

4

There is a speedup, but making it work for such a small case is harder than just brute force.

The speedup relies on knowing the leading digit we're looking for, and only looking at numbers that give the right trailing digit. With more work on larger numbers, you can match the last two to the first two. And so on. And now we're only generating a small fraction of the triangular numbers to look for palindromes.

To determine the output mod 5, you need the input mod 5.

0  0
1  1
2  3
3  1
4  0

Note, that mod 4 congruence shows that the trailing digit can only be in $0, 1, 3, 5, 6, 8$. The same must be true of the leading digit.

To determine the output mod 2, you need the input mod 4.

0  0
1  1
2  1
3  0

Note, from the mod 5 rule we've gotten rid of 5 out of 9 trailing digits. That means that we've also gotten rid of 5 out of 9 trailing digits.

Now the triangular numbers start off $0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91$. That takes us up to $t_{13}$.

Searching through those with leading 100, we have two rules. Mod 5 it must be 1 or 3. Mod 4 it must be 1 or 2. So mod 20 the input must be $1, 3, 6, 11, 13, 18$. We're already at 13, so jump to 18 and we get 171. Nice. Now jump to 21...oops, too far.

Now we jump to the next leading digit. 3. Our rules are 2 mod 5, and 1 or 2 mod 4. Mod 20 this gives $2, 17$ mod 20. Try 37, too far.

Let's go back. Our leading digit can be 5. The input mod 20 has to be 5, 9, 10, or 14. Skipping forward, $t_{34}$ gives us 595.

Now you're going into the 600s. What are the mod rules for 5 and 4? What does that work out to mod 20? What numbers to try?

After the 600s, you skip to the 800s. (Why?)

If you were to keep going, the same set of rules would work great through the 3 and 4 digit numbers. But by the time you get to the 5 digit numbers, you can split our lists of mod 20 inputs for mod 10 outputs, into lists of mod 200 inputs for mod 10 outputs. And now we can proceed by advancing the leading two digits, and only looking at inputs that create the right last two digits. Speeding us up more.

While it is complicated, and absolutely not worth it for such a small list, this would be a huge speedup for a program that was finding all of them up to, say, a billion.

btilly
  • 3,671
  • 1
    Presumably this sort of thing makes a big difference for finding numbers like $8215588527084263951180440811593624807258855128$ – Henry Sep 22 '25 at 12:15
3

A small speedup to btilly's answer : using the fact that all even digit palindromic numbers must be multiples of 11, means that after n = 3, we begin with 2 digit numbers so we jump to n = 10 and n = 11 (the only possibilities by prime factorisation) till we exceed 100.

Hevidu
  • 41
2

Well, for single digits we need $n=\frac{k(k+1)}2 \le 9$ or $k=1,2,3$ and $n=1,3,6$

For two digits we need $n=\frac{k(k+1)}2=(m\pm 1)\frac m2=a\cdot 11$ for some even number $m$ and $a$ a single digit. So $11\mid m\pm 1$ and $m=2a$. So $m=5$ and $n =55$.

For three digits, we have ... well... I can't really come up with anything so slick with $n = \frac {k(k+1)}2 = 101a + 10b$.

But for $n=\frac {k(k+1)}2 \ge 100$ we have $k\ge 14$ and $T_{14} = \frac {14\cdot 15}2 =105$. Then for the next several numbers we add $15 + 16 +17 + 18 + 19 + 20$ and doing some modding $\pmod {10}$ we get last digits of $0,6,3,!!!1!!!, 0,10$ so $T_{18}=\frac{18\cdot 19}2 = 171$.

Then we must add $19 + 20$ to get $T_{20} = 210$ and we must add the next several numbers $21,22,23,24$ and $\pmod{10}$ to get last digits of $1,3,6,10$ and as $21+22+23+24=80 + 10=90$ and $210+90=300$ we get $T_{24} = \frac{24\cdot 25}2=300$ and there were no palindromic ones.

Adding the next few terms $25+26+27+28$ we get last digits of $6,1,8,6$. No palindromes and $25+26+27+28 = 100 + 6$ so $T_{28}= 406$.

Next few terms: $29+30 +31$ we get last digits $5,5,6$. No good. $29+30+31=3\times 30 = 90$ so $T_{31}=496$.

The next few terms: $32+33+34$ and we get last digits of $8,11,!!!5!!!$ so $T_{34}= \frac {34\cdot 35}2 = 595$.

The next few terms $35+36+37$ give us final digits of $0, 6, 3$ (no palindromes) with $35+36+37= 99 + 2+3+4 = 108$ so $T_{37}=703$

Next few terms: $38+39+40$ give us final digits of $1,0,0$ and $T_{40}= \frac {40\cdot 41}2 = 820$.

Next two digits by adding $41+42$ fail. and $T_{42}=903$

Next two terms $43$ and $44$ give us final digits of $6$ and $0$ so no more palindromes

fleablood
  • 134,278
0

Most likely, given the small range, it is probably best to just compute and test all 44 triangular numbers.

But if you are working with a computer that represents numbers in base two, there is a way to reduce the need for conversions to base ten once the triangular number reaches three digits (which starts with the 14th triangle). You count the bits and see if that accords properly with a terminal digit you infer in advance.

Begin by setting up the"wheel" of terminal digits if triangular numbers:

1-3-6-0-5-1-8-6-5-5-6-8-1-5-0-6-3-1-0-0

With each triangular number computation you move one spot forward on this wheel.

Now when the triangular number reaches three digits, the hundreds digit must match the digit indicated by your position on the wheel. This constrains the number of bits the binary representation will have from the leading 1 to its units place:

Ends with 1 $\implies$ 100-199 $\implies$ 7 or 8 bits

Ends with 3 $\implies$ 9 bits

Ends with 5 $\implies$ 9 or 10 bits

Ends with 6 or 8 $\implies$ 10 bits.

For instance, at the 16th triangular numbers you point to 6 on the wheel but the binary representation of the number has only eight bits (10001000), so conversion of the triangular number to base ten is useless. With the 18th triangular number the wheel says 1 and the computed binary triangular number has eight bits (10101011), which may work and thus warrants the conversion to base ten for that case.

Oscar Lanzi
  • 51,942