
Any positive number can be written as a sum of three palindromes - akudha
http://somethingorotherwhatever.com/sum-of-3-palindromes/
======
CydeWeys
For those wondering, this works in every base ≥ 5, according to the original
paper:
[https://arxiv.org/abs/1602.06208v2](https://arxiv.org/abs/1602.06208v2)

Every time I see something like this that is dependent on base-10
representation, I'm always curious to know whether it generalizes, or if it's
a quirk specific to base-10 representation. For example, being able to tell if
a number is divisible by 2 or 5 by only looking at the last digit.

~~~
robotmlg
And according to the Numberphile video
([https://www.youtube.com/watch?v=OKhacWQ2fCs](https://www.youtube.com/watch?v=OKhacWQ2fCs))
there is a prior paper showing it for bases 2, 3, and 4

~~~
jerf
Not base 2. All palindromes in base 2 have a 1 in the last position, because
there's a 1 in the first position. This means that any addition of 3
palindromes must be odd because the result of the addition is guaranteed a 1
in the ones position, meaning even numbers can't possibly be represented that
way. This is discussed in the original paper at the bottom of the second page:
[https://arxiv.org/pdf/1602.06208.pdf](https://arxiv.org/pdf/1602.06208.pdf) ,
which I've expanded out a bit since they just mention the evenness without the
argument I give. They say base 2 needs at least 4 summands, but their phrasing
suggests to me that's intended as a lower bound, not that they have a proof 4
is always sufficient. (Not to mention it would have to be more complicated
than that anyhow since it would have to be something like "3 for odd and 4 for
even above some threshold", or some other bounds.)

~~~
fourthark
This site uses 0.

------
brk
It seems to go for the "least interesting" result. eg: I gave it 363 and it
gave me back 363 + 0 + 0, which while technically true, 121 + 121 + 121 would
have been more in the spirit of it, IMO. Or even 242 + 121 + 0 would be a
little easier than just reusing the original palindrome number with zeros.

~~~
tim1994
Well it proofs the existence and there are many special cases they consider.
If the number is a palindrome already there are many possible solutions. Using
zeros is a trivial an the simplest solution. Using anything else would be
abitrary and longer too.

------
akudha
From this numberphile video -
[https://www.youtube.com/watch?v=OKhacWQ2fCs](https://www.youtube.com/watch?v=OKhacWQ2fCs)

------
kodablah
Looks like code is at [0]. Didn't read in detail, but how difficult is it to
find these 3 palindromes for something like a 100 digit number? Or, asked
another way, how hard will it be to crack my new 3-palindrome-sum-factor based
encryption scheme (assuming I have rules about not having easy factors)?

0 - [https://github.com/christianp/sum-of-
palindromes](https://github.com/christianp/sum-of-palindromes)

~~~
CalChris
Uniqueness would be a problem. The sum isn't guaranteed to have unique terms.

2 is (2,0,0) and (1,1,0).

~~~
gilleain
Huh, so its related to the problem of finding the restricted weak compositions
of a number.

A composition is an ordered partition. A partition is the set of numbers that
sum to a particular number. The 'weak' part allows zeros.

------
tw1010
Wonderful wonderful website.

What happends to quirky people like this when they leave university and enter
the industry?

~~~
nmg
Is there such a thing as "interactive nonfiction"? Because if not, there
should be. Design educational websites this way and watch children learn.

~~~
dragonwriter
> Is there such a thing as "interactive nonfiction"?

The real physical Universe is interactive nonfiction.

~~~
TomMarius
The real physical universe doesn't offer tooling for playing with advanced
math, though.

~~~
JadeNB
> The real physical universe doesn't offer tooling for playing with advanced
> math, though.

The real physical universe is nothing _but_ tooling for playing with advanced
math (but it's not always easy to figure out what the advanced math is, or how
best to play with it).

~~~
TomMarius
IMHO that's like saying higher level programming languages than assembly are
unnecessary. Or that a debugger is useless.

And not just the learning purposes. Imagine the possibilities if we could
properly simulate (and speed up the simulation) the world - what if we could
use genetic algorithms to develop an organism.

~~~
JadeNB
> IMHO that's like saying higher level programming languages than assembly are
> unnecessary. Or that a debugger is useless.

These two statements are quite different. Higher level programming languages
than assembly _are_ unnecessary; anything beyond the lambda calculus is
unnecessary (assuming our goal is to compute Turing computable things). That's
very different from saying that they're useless; just because they _can_ be
done without doesn't mean that they _should_.

~~~
TomMarius
I know that these statements are quite different, I deliberately made two
different examples - one about creating and one about debugging. A world
simulator is going to be a test environment for us as well as an introspection
tool.

------
projectramo
For those who are not impressed when they enter "3" and "444", try something
like "8239419503490293592", and prepare to be blown away.

~~~
lucb1e
I figured it had small cases down so I immediately went for a fifteen digit
number from /dev/urandom and was happy that it solved it perfectly. But I'm
probably the exception.

For those wondering how to generate random numbers, it's simple if you don't
care about a little computational overhead: </dev/urandom tr -dc 0-9 | head -c
15 (substitute 0-9 for any character set like a-zA-Z or \ -~). I use this to
generate random PIN numbers after it took too long to read the pwgen man page,
and I might start to use it as replacement for pwgen altogether since it only
uses very basic tools that are available everywhere, unlike pwgen.

To explain the command: we read urandom into tr's stdin; tr translates (like
"echo haha | tr a u"); with -d, tr deletes instead of substituting; with -c,
it takes the compliment set (so everything except what you specify). Basically
we're filtering characters or of urandom's output. Finally, we limit the
output to a certain number of characters.

~~~
EForEndeavour
Cool one-liner to get random digits. On OSX, it produces the error "tr:
Illegal byte sequence." The solution, per
[https://unix.stackexchange.com/q/45404](https://unix.stackexchange.com/q/45404),
is to prepend the script with "LC_CTYPE=C ":

LC_CTYPE=C </dev/urandom tr -dc 0-9 | head -c 15

------
tzs
If a(n) is the number of ways to write n as the sum of three palindromes, the
sequence a(0), a(1), a(2), a(3) is kind of interesting.

[http://oeis.org/A261132](http://oeis.org/A261132)

~~~
JadeNB
> If a(n) is the number of ways to write n as the sum of three palindromes,
> the sequence a(0), a(1), a(2), a(3) is kind of interesting.

Do you just mean "the sequence a(n)"? I don't see anything especially
interesting about the first 4 terms.

~~~
tzs
Yeah, a ", ..." got left out.

------
arayh
I like how the JavaScript functions support different bases (5 or greater),
but the page itself doesn't. It makes me want to play around with it in the
browser console...

------
et2o
Every positive number is a sum of infinite palindromes since they include 0 as
a palindrome. Try entering 101.

~~~
jarfil
It's easier to find more than fewer palindromes that when summed up will give
a certain number. Finding just the three, is not that easy.

~~~
madcaptenor
And just to clarify, the greedy algorithm (take the largest palindrome less
than the number you're trying to express) doesn't work. To take a random nine-
digit example (literally the first one I tried): 635932028 = 635929536 + 2442
+ 44 + 6. In general you can express N as a sum of something like log(log(N))
palindromes this way, which is not quite constant.

------
torstenvl
Literally just resets and refreshes the page when I hit the submit.

~~~
aembleton
You need to allow Javascript

------
croddin
Small nitpick

> Yes, I can do this for any whole number bigger than zero.

Should say "Yes, I can do this for any whole number zero or bigger." since 0
is a palindrome

------
DoofusOfDeath
This reminds me a bit of Fourier transformations from time- to frequency-
domain.

Where an arbitrarily complex time-series signal can be accurately represented
as the sum of various sine waves.

Except the OP deals with a discretized values, not real-number values, and the
OP's approach lets the constituent palindromes start/end at an arbitrary
offset, rather than having to be repeated continuously.

------
sigmaprimus
The most impressive part of this calculator IMHO is the animated text, it
dresses up a parsing script and function into a very nice package. I say good
job, I wonder how difficult it would be to show a list of all the combinations
of three "palindromes" for a given number?

------
markstos
Could this trick be used to improve compression? Since palindromes contain
repeated digits by definition, perhaps they are more compressible than the
original number? Of course, you'd have three numbers to compress instead of
one...

~~~
yorwba
Palindromes can be compressed 50% by using their symmetry. Representing a
number as 3 palindromes would "compress" a number to 1.5 times the original
size in the worst case. Obviously not an improvement.

If the number itself happens to be a palindrome, you can get down to 50%, but
that's completely unrelated to this trick.

In general, it's impossible for a compression algorithm to losslessly compress
all numbers to a shorter representation, it can only make some numbers shorter
and others longer. (Pigeonhole principle: there are not enough short codes, so
if you assign a shorter code to all numbers, some will get the same code,
making the compression not lossless.)

Practical uses of compression are all about reducing the average length given
a nonuniform probability distribution, so a palindrome-based compression
scheme would only be useful if you could expect most numbers you're dealing
with to be palindromes.

------
dominotw
Prepare to see in this question in programming waterboarding interviews.

------
malmsteen
42 pages <insert scared emoji>.

in all seriousness it is always amazing to see simple theorems rely on such
complex proofs

------
naringas
i'm underwhelmed

77777777777777 +0 +0 =77777777777777

~~~
jaxtellerSoA
yeah....I don't see why this such an "impressive" trick.

~~~
dsjoerg
OK tough guy, please give me three palindromic numbers that add up to
1,234,567. And don't use the trick! And don't use brute force!

~~~
jaxtellerSoA
1,234,567 + 0 + 0 = 1,234,567

~~~
JadeNB
> > OK tough guy, please give me three palindromic numbers that add up to
> 1,234,567. And don't use the trick! And don't use brute force!

> 1,234,567 + 0 + 0 = 1,234,567

The first of these is not a palindrome.

------
ez15972
I entered 55555 which gave the underwhelming result of 55555+0+0 :/

~~~
relate
Would you prefer 11111+11111+33333 instead?

~~~
rejschaap
Still a bit boring, feels like cheating, something like 12321 + X + Y would be
nice.

~~~
ashtonbaker
55555 = 12321 + 43234 + 0

:)

And if you don't like that:

55555 = 12321 + 23132 + 20102

Palindromes are sort of the least surprising numbers to have this property
because you can easily "split" them into as many palindromes as you want like
this. The surprising result is that you could write something like
19837100018374 as the sum of _only_ 3 palindromes.

~~~
kosievdmerwe
More surprising is that you can do it in any base >= 5

------
richmarr
Why three? Anyone have a theory (or the answer)?

~~~
gnulinux
Well, there is a whole 40+ page paper on the algorithm and its proof of
correctness. So, yes there is an answer.
[https://arxiv.org/abs/1602.06208](https://arxiv.org/abs/1602.06208)

~~~
richmarr
The intent of my question was an answer that doesn't require reading 40 pages
of mathematics proof (and before that learning _how_ to read 40 pages of
mathematics proof).

------
amelius
In any base?

~~~
Jaxan
Yes the theorem holds for any base.

------
rekshaw
1000 = 999 \+ 1 \+ 0 \--- That don't impress me much.

~~~
happytoexplain
Why?

