## A278328

The On-Line Encyclopedia of Integer Sequences (also known by its acronym, OEIS) is a database hosting hundreds of thousands of — as the name implies — integer sequences. Yet, despite the massive number of entries, I contributed a new integer sequence, A278328.

A278328 describes numbers whose absolute difference to their decimal reverse are square. An example would be $12$ or $21$ (both are the decimal reverse to each other), since $\left|12-21\right|=9$ and $9=3^2$.

Not a whole lot is known about the sequence, partly due to its definition only resulting in the sequence when using the decimal system, though it is known that there are infinitely many numbers with said property. Since there are infinitely many palindromes (numbers whose reverse is the number itself), $\left|n-n\right|=0$ and $0=0^2$.

Due to there — to my knowledge — not being a direct formula for those numbers, I wrote a Python script to generate them. On the sequence’s page, I posted a program which endlessly spews them out, though I later wrote a Python two-liner, which only calculates those members of the sequence in the range from $0$ to $98$ (shown below entered in a Python shell).

>>> import math
>>> filter(lambda n:math.sqrt(abs(n-int(str(n)[::-1])))%1 == 0, range(99))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 15, 21, 22, 23, 26, 32, 33, 34, 37, 40, 43, 44, 45, 48, 51, 54, 55, 56, 59, 62, 65, 66, 67, 73, 76, 77, 78, 84, 87, 88, 89, 90, 95, 98]

## Palindrome Function

To get a number’s palindrome in a programming language like python is easy. There are ways to swap between integer and string and strings can be manipulated.

>>> n = 1234
>>> int(str(n)[::-1])
4321

But I wanted to create a mathematical function $p(n)$, which returns an integer’s palindrome. Thus $p(1234) = 4321$.

Firstly I needed a way of determining the number’s size. In base $10$ the length is calculated using the logarithm to said base.
$l(n) = \lfloor \log_{10}{n} \rfloor + 1$
$l(1234) = \lfloor \log_{10}{1234} \rfloor = \lfloor3.09 \rfloor + 1 = 4$

Secondly I need a way to isolate a specific digit. Using the floor function, this function returns the $i\text{-th}$ digit (starting on the right with $i=0$).
$d_i(n) = \lfloor \frac{n}{10^i} \rfloor - \lfloor \frac{n}{10^{i+1}} \rfloor \cdot 10$
$d_2(1234) = \lfloor \frac{1234}{10^2} \rfloor - \lfloor \frac{1234}{10^{2+1}} \rfloor \cdot 10 = \lfloor 12.34 \rfloor - \lfloor 1.23 \rfloor \cdot 10 = 12 - 1 \cdot 10 = 2$

Thirdly both of these functions can be used to split up the number into a sum.
$n = \sum\limits_{i=0}^{l(n)-1} \Big[ d_i(n) \cdot 10^{i} \Big] = \sum\limits_{i=0}^{\lfloor \log_{10}{n} \rfloor} \Big[ \big( \lfloor \frac{n}{10^i} \rfloor - \lfloor \frac{n}{10^{i+1}} \rfloor \cdot 10 \big) \cdot 10^{i} \Big]$

Fourthly I only need to swap the power of ten at the end to get my palindrome function.
$p(n) = \sum\limits_{i=0}^{l(n)-1} \Big[ d_i(n) \cdot 10^{l(n) - 1 - i} \Big] = \sum\limits_{i=0}^{\lfloor \log_{10}{n} \rfloor} \Big[ \big( \lfloor \frac{n}{10^i} \rfloor - \lfloor \frac{n}{10^{i+1}} \rfloor \cdot 10 \big) \cdot 10^{\lfloor \log_{10}{n} \rfloor - i} \Big]$

Thus the final function $p(n)$ is defined.
$p(n) = \sum\limits_{i=0}^{\lfloor \log_{10}{n} \rfloor} \Big[ \big( \lfloor \frac{n}{10^i} \rfloor - \lfloor \frac{n}{10^{i+1}} \rfloor \cdot 10 \big) \cdot 10^{\lfloor \log_{10}{n} \rfloor - i} \Big]$

To check if the formula is correct, I use $1234$ (as seen above).
$p(1234) = \sum\limits_{i=0}^{\lfloor \log_{10}{1234} \rfloor} \Big[ \big( \lfloor \frac{1234}{10^i} \rfloor - \lfloor \frac{1234}{10^{i+1}} \rfloor \cdot 10 \big) \cdot 10^{\lfloor \log_{10}{1234} \rfloor - i} \Big]$
$p(1234) = \sum\limits_{i=0}^{3} \Big[ \big( \lfloor \frac{1234}{10^i} \rfloor - \lfloor \frac{1234}{10^{i+1}} \rfloor \cdot 10 \big) \cdot 10^{3 - i} \Big]$
$p(1234) = d_0(1234) \cdot 10^3 + d_1(1234) \cdot 10^2 + d_2(1234) \cdot 10^1 + d_3(1234) \cdot 10^0$
$p(1234) = 4000 + 300 + 20 + 1 = 4321$

## Palindromic Primes

TheOnlinePhotographer has published a post to celebrate 171717 comments and was amused by the number’s symmetry.
A great comment by Lynn pointed out that this number is indeed an interesting number but not symmetrical.
Symmetrical numbers or words also called palindromes are defined as being the same read forwards or backwards. Examples for palindromic words are radar, noon or level. Palindromic numbers are 3, 404 or 172271.

Lynn then went further and checked if 171717 is at least a prime. The number sadly has five distinct prime factors $(171717 = 3 \cdot 7 \cdot 13 \cdot 17 \cdot 37)$.

So Lynn wondered what the next palindromic prime would be.
To answer this question, I wrote this little Python program to check for palindromic primes. The first 120 palindromic primes are shown below.
Based on this list, the smallest palindromic prime larger than 171717 is 1003001.

      3,       5,       7,      11,     101,     131,     151,     181,
191,     313,     353,     373,     383,     727,     757,     787,
797,     919,     929,   10301,   10501,   10601,   11311,   11411,
12421,   12721,   12821,   13331,   13831,   13931,   14341,   14741,
15451,   15551,   16061,   16361,   16561,   16661,   17471,   17971,
18181,   18481,   19391,   19891,   19991,   30103,   30203,   30403,
30703,   30803,   31013,   31513,   32323,   32423,   33533,   34543,
34843,   35053,   35153,   35353,   35753,   36263,   36563,   37273,
37573,   38083,   38183,   38783,   39293,   70207,   70507,   70607,
71317,   71917,   72227,   72727,   73037,   73237,   73637,   74047,
74747,   75557,   76367,   76667,   77377,   77477,   77977,   78487,
78787,   78887,   79397,   79697,   79997,   90709,   91019,   93139,
93239,   93739,   94049,   94349,   94649,   94849,   94949,   95959,
96269,   96469,   96769,   97379,   97579,   97879,   98389,   98689,
1003001, 1008001, 1022201, 1028201, 1035301, 1043401, 1055501, 1062601, ...

Thus it takes $1003001 - 171717 = 831284$ more comments to reach the closest palindromic prime.

The sequence of palindromic primes is number A002385 in the On-line Encyclopedia of Integer Sequences (OEIS).

# Python 2.7.7 Code
# Jonathan Frech 23rd of March, 2016