通过加1计算回文数

回文数怎么计算

通过加1计算回文数 (Counting Palindromes By Adding 1)

One morning, while walking, I saw a building with the number 13231, and I identified that number as a palindrome (a number that is the same when the digits are read forwards and backwards) and thought, “What number palindrome is that?

一天早晨,在散步时,我看到一幢编号为13231的建筑物,并确定该编号为回文数(当数字向前和向后读取时,该数字是相同的),然后想到:“那是什么回文数?

To see what I mean by that, here is a list of the first 22 palindromes:

要了解我的意思,以下是前22条回文列表:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121…

0、1、2、3、4、5、6、7、8、9、11、22、33、44、55、66、77、88、99、101、111、121…

The “palindrome number” of any palindrome will be where it is found in this list. For example, I will call 0 the first palindrome, 1 the second, 9 the tenth, 11 the eleventh, and 22 the twelfth, and so on. This numbering is equivalent to the number of palindromes less than or equal to the palindrome. For example, 131 has these 23 palindromes less than or equal to it:

任何回文的“回文数”将在此列表中找到。 例如,我将第一个回文命名为0,将第二个回文命名为1,将第十个回文命名为9,第十一个为11,第十二个为22,依此类推。 该编号等于小于或等于回文的回文数。 例如,131个以下23个回文数小于或等于它:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131

0、1、2、3、4、5、6、7、8、9、11、22、33、44、55、66、77、88、99、101、111、121、131

Therefore, 131 is the 23rd palindrome. This same logic can extend to our target number (13 231), but requires a much longer list. To count the palindromes less than or equal to 13231, I first counted the one-digit, two-digit, three-digit, and four-digit palindromes, giving me the number of palindromes below 10000. After this, I simply had to add the number of palindromes between 10000 and 13231, which should be easier to find.

因此,131是第23回文。 相同的逻辑可以扩展到我们的目标数字(13 231),但需要更长的列表。 要计算小于或等于13231的回文数,我首先对一位,两位,三位和四位回文数进行计数,得出的回文数低于10000。此后,我只需要添加回文数介于10000和13231之间,应该更容易找到。

One-digit palindromes are easy — there are 10, the numbers 0–9. Two-digit palindromes are almost as easy; we can start the number with any digit but 0 (because then it would no longer be two-digit), and the second number must be the same as the first. This leaves us 9 two-digit palindromes: 11, 22, 33, 44, 55, 66, 77, 88, and 99.

一位数的回文数很容易-有10个,数字0-9。 两位数的回文几乎一样容易。 我们可以使用除0之外的任何数字开头数字(因为它将不再是两位数字),并且第二个数字必须与第一个数字相同。 这使我们得到9个两位数回文,即11、22、33、44、55、66、77、88和99。

With three-digit palindromes, the first and last digits must be the same, while the middle digit can be anything. As before, the first (and last) digit cannot be 0, leaving us with 9 choices for a starting (and ending) digit. The middle digit, however, has no limitations; it can be any of the 10 digits from 0–9. The total number of palindromes can be seen as (number of choices for first digit) x (number of choices for middle digit), where solving gives 9 x 10 = 90 three-digit palindromes.

对于三位数回文,第一位和最后一位必须相同,而中间位可以是任何数字。 和以前一样,第一个(和最后一个)数字不能为0,因此我们有9个开始(和结束)数字选择。 但是中间的数字没有限制。 它可以是0到9之间的10个数字中的任何一个。 回文的总数可以看作((第一个数字的选择数)x(中间的数字的选择数)),其中求解得出9 x 10 = 90个三位数回文。

Four-digit palindromes are exactly the same: 9 choices for the first and last digit, 10 choices for the middle two (which must be the same). Again multiplying, this gives us 90 four-digit palindromes.

四位数回文完全相同:第一个数字和最后一个数字9个选择,中间两个数字10个选择(必须相同)。 再次相乘,就得出了90个四位数的回文。

This, gives us the total number of palindromes below 10000. 10 one-digit + 9 two-digit + 90 three-digit + 90 four-digit = 199 palindromes below 10000. To get the the answer to our original question, we have to add the number of palindromes between 10000 and 13231, listed below.

这样,我们得出的回文总数低于10000。10个一位数+ 9个两位数+ 90个三位数+ 90个四位数= 199个低于10000个回文数。要得到我们原始问题的答案,我们必须添加下面列出的回文数在10000和13231之间。

10001, 10101, 10201, 10301, 10401, 10501, 10601, 10701, 10801, 10901, 11011, 11111, 11211….

10001、10101、10201、10301、10401、10501、10601、10701、10801、10901、11011、11111、11211…。

Interesting… if you look at the first three digits (which are reflected into the last three), they seem to be counting up. In fact, it makes sense that they are because we are always looking for the next palindrome, which is just the next natural number and the number’s reflection! This gives us an easy answer to how many palindromes are between 10000 and 13231; simply take the first three digits (the repeated part of the palindromes) and count the numbers in that range. To do this, simply subtract 100 from 132 and add 1. This means there are 33 palindromes between 10000 and 13231 (including 13231).

有趣的是……如果您查看前三个数字(反映在后三个数字中),它们似乎正在增加。 实际上,它们之所以有意义是因为我们一直在寻找下一个回文,这只是下一个自然数和该数的反映! 这使我们可以轻松地回答10000至13231之间有多少回文; 只需取前三个数字(回文的重复部分)并计算该范围内的数字即可。 为此,只需从132中减去100并加1。这意味着在10000和13231之间(包括13231)有33个回文。

Our final answer is then the 199 palindromes below 10,00 + the 33 above = 232 palindromes less than or equal to 13231. Thus 13231 is the 232nd palindrome, a fitting number that itself is the 33rd palindrome, which itself is the 13th.

那么,我们的最终答案是,低于10,00的199个回文+小于或等于13231的232个回文。因此,第232个回文是13231,一个合适的数字本身就是第33个回文,它本身就是第13个。

Now the curious among you may be asking if there is a an easier way to find this answer, and, of course, the answer is yes. We found the palindrome number of 13231, but what about any other number?

现在,您中间的好奇者可能会问,是否有更简单的方法来找到此答案,当然,答案是肯定的。 我们发现回文数为13231,但是其他数字呢?

Let us move away from a specific example and instead think about a palindrome P that we wish to find the palindrome number of. To be clear, the value we are looking for is the number of palindromes less than or equal to P.

让我们远离一个具体的例子,而是考虑一个我们希望找到其回文数的回文P。 明确地说,我们正在寻找的值是小于或等于P的回文数。

Similar to the example above, it is easier to break this counting down into finding the palindromes with a certain number of digits and adding these up until we get close to P. For example, we added the number of one, two, three, and four-digit palindromes to get the number of palindromes under 10000, which is the largest multiple of 10 under 13231. This method will get us much closer to our target and will make calculation easier.

与上面的示例类似,将计数倒数分解为找到具有一定位数的回文数并累加起来直到我们接近P更为容易。例如,我们将数字1、2、3和四位数回文数可得出10000以下的回文数,这是13231下10的最大倍数。这种方法将使我们更接近目标,并使计算更加容易。

We know the number of palindromes with one digit is 10, two digits is 9, three digits is 90, and four digits is also 90. Counting five-digit palindromes is easy too, 9 choices for the first (and last) digit, 10 choices for the second (and fourth) digit, and 10 choices for the middle digit, giving 9 x 10 x 10 = 900 five-digit palindromes. Identically, we can calculate that there are also 900 six-digit palindromes.

我们知道一位回文数为10,两位为9,三位为90,四位也为90。计算五位回文数也很容易,第一个(最后一个)数有9个选择,10第二个(和第四个)数字选择,中间的数字选择10个,得到9 x 10 x 10 = 900个五位数回文。 同样,我们可以计算出也有900个六位回文。

So our sequence so far is: 10, 9, 90, 90, 900, 900…

到目前为止,我们的顺序是:10、9、90、90、900、900…

This pattern of number of palindromes per digit is very interesting. It seems that the numbers repeat twice (excluding the two at the start), and then scale by 10. The scaling behavior can be confirmed by noticing that each digit of a palindrome has a reflected copy on the other end of the number (1654561), so adding two more digits adds one more pair of these digits, giving 10 choices for the new digit and increasing the total number of palindromes by 10 times.

每位数回文数的这种模式非常有趣。 似乎数字重复两次(开始时不包括两个数字),然后按10进行缩放。可以通过注意到回文的每个数字在数字的另一端都有反射副本来确认缩放行为(1 6 545 6 1),因此再增加两个数字就可以再增加一对数字,为新数字提供10个选择,并将回文总数增加10倍。

The repeated behavior of this sequence is explained by thinking of the ‘base number’ of a palindrome, the number that is being being reflected. For example, the base number of 13255231 is 1325, and the base number of 121 is 12. Every palindrome has a base number, and every base number can be made into two palindromes. The base number 1325 can be made into 13255231 or 1325231, depending on if the middle digit is ‘shared’ between the base and its reflection. This idea of shared and unshared palindromes will become extremely helpful. In this case, it is obvious that there are the same number of 7-digit and 8-digit palindromes because they all come from the same four-digit base numbers. This same logic applies to the number of (2n-1)-digit palindromes and (2n)-digit palindromes.

通过考虑回文的“基数”来解释此序列的重复行为,该基数正在被反映。 例如,13255231的基数是1325,121的基数是12。每个回文塔都有一个基数,每个基数都可以组成两个回文塔。 可以将基数1325设置为132 55 231或132 5 231,具体取决于中间数字是否在基数及其反射之间“共享”。 共享和非共享回文的想法将非常有用。 在这种情况下,很明显,存在相同数量的7位和8位回文,因为它们都来自相同的四位基数。 此逻辑适用于(2n-1)位回文数和(2n)位回文数。

But that still leaves the question of the start of the sequence — Why are there 10 one-digit palindromes and not 9? The answer is 0. We normally think of 0 as a one-digit number, but it doesn’t really fit in, as we normally do not allow palindromes to start with 0. This results in us having an extra one-digit number, and doesn't affect much else.

但这仍然是序列开始的问题-为什么会有10个一位数回文而不是9个? 答案是0。我们通常将0视为一个数字,但实际上并不合适,因为我们通常不允许回文以0开头。这导致我们有一个额外的数字,并不会影响太多。

Our sequence of number of palindromes has now been confirmed to follow the patterns we identified, and starts: 10, 9, 90, 90, 900, 900, 9000, 9000… We also know that any palindrome with an odd number of digits is ‘shared’ while with even number of digits it is ‘unshared.’

现在已确认我们的回文数顺序遵循我们确定的模式,并开始于:10、9、90、90、900、900、9000、9000…我们还知道,任何具有奇数位数的回文为'共享”,而偶数位数则表示“未共享”。

For example, let’s say P is 5839229385. This palindrome has 10 digits and is unshared, as seen by the duplicate 2s in the middle. To find the total number of palindromes less than or equal to our palindrome P, we can add together the number of shared and unshared palindromes below P. To get the number of shared palindromes below P, we need to add all of the numbers in our pattern corresponding to odd-length (but with less than 10 digits) palindromes: 10, 90, 900, 9000, and 90000. Adding these together gives us 100000, a remarkably nice number of shared palindromes. Generalizing this, this number is always a power of ten, with the number of digits being one more than half the number of digits in the palindrome (if the palindrome is unshared).

例如,假设P为5839229385。此回文数为10位,并且是不共享的,如中间的重复2s所示。 要找到小于或等于我们的回文数P的回文总数,我们可以将P以下的共享和非共享的回文数相加。要使P之下的共享回文数不超过P,我们需要将所有的相加数相加对应于奇数长度(但少于10个数字)回文的模式:10、90、900、9000和90000。将这些总和相加得出的结果为100000,这是共享回文的非常多的数目。 概括地说,此数字始终是10的幂,数字的数目是回文数(如果回文是非共享的)数的一半以上。

To find the number of unshared palindromes less than or equal to P, we will use a different method. The base number of our example (5839229385), is 58392. Any base number below 58392 will correspond to a palindrome below P if we construct it as unshared, and any base number above 58392 will correspond to a palindrome above P. We can use this fact to see that there are, in fact, 58392 (the base number) unshared palindromes less than or equal to P. Generalizing again, the base number for an unshared palindrome is exactly half the length of the palindrome itself.

为了找到小于或等于P的未共享回文数,我们将使用另一种方法。 我们的示例(5839229385)的基数是58392。如果我们将其构造为非共享,则低于58392的任何基数都将对应于P以下的回文,如果高于58392的任何基数将对应于P之上的回文。事实证明,实际上有58392(基数)个未共享的回文数小于或等于P。再次概括一下,一个不共享的回文数的基数恰好是回文长度的一半。

Adding together the 100000 shared palindromes below P and the 58392 unshared palindromes below or equal to P, we get that P is the 158392nd palindrome. Generalizing this, when we have any unshared palindrome, if you add a 1 to the beginning of the base number of a palindrome, you have the number of that palindrome. Testing this, 1331 is the 113th palindrome, 845548 is the 1845th palindrome, and 11 is the 11th palindrome. This, however, does not work for shared palindromes.

将P以下的100000个共享回文和小于或等于P的58392个非共享回文加在一起,我们得出P是第158392个回文。 概括地说,当我们有任何未共享的回文数时,如果在回文数的基数开头添加1,则您具有该回文数。 经过测试,1331是第113回文,845548是1845回文,而11是第11回文。 但是,这不适用于共享回文。

For unshared palindromes, “1” + the base number is the palindrome number.

对于不共享的回文数,“ 1” +基数是回文数。

Do not worry, shared palindromes have a similar (and nearly as easy) method of finding their number. For this, we will consider 0 to be two-digits, as it makes our math slightly easier and won’t affect results. Our new palindromes by length pattern is: 9, 10, 90, 90, 900..., the same but with the first two elements switched.

不用担心,共享回文法具有类似(几乎一样容易)的数量查找方法。 为此,我们将0视为两位数,因为这会使我们的数学运算更简单,并且不会影响结果。 我们的新回文按长度模式是:9、10、90、90、900 ...,相同,但是前两个元素已切换。

To find the number of unshared palindromes below a shared palindrome P with a number of digits D, we add the numbers corresponding to unshared (and thus even-length) items in the patten that are below D digits: 10, 90, 900, 9000, etc. Again, we always get a multiple of 10 with a length of (D-1)/2.

为了找到共享回文数P之下具有数位D的未共享回文数,我们将对应于D位数以下的未共享(因此为偶数长度)项目的数字添加到D中:10、90、900、9000等等。同样,我们总是得到10的倍数,长度为(D-1)/ 2。

To find the number of shared palindromes less than or equal to a shared palindrome P, it is exactly the same as the process to find the number of unshared palindromes less than or equal to a a unshared palindrome — that is, it is equal to the base number. This time, however, the base number has (D+1)/2 digits.

要查找小于或等于共享回文数P的共享回文数,它与查找小于或等于一个非共享回文数的非共享回文数的过程完全相同-即,它等于基数。数。 但是,这一次,基数有(D + 1)/ 2位数字。

Again, we find ourselves adding a power of ten to the base number of the palindrome, but it is no longer a nice concatenation. Instead of simply placing the 1 in front of the base number, the power of 10 lines up to add a one to the first digit of the base number. Trying this, 34543 is the 445th palindrome, 9 is the 10th palindrome, and 13231 is the 232nd palindrome.

同样,我们发现自己在回文的基数上增加了10的幂,但是它不再是一个很好的串联。 与其简单地将1放在基数前面,不如将10的幂乘以在基数的第一位加一个。 尝试此操作时,第34回文是34543,第10回文是9,第232回文是13231。

For shared palindromes, the base number with 1 added to the first digit is the palindrome number.

对于共享回文,在第一位加1的基数是回文数。

With that, I think I’ve thoroughly covered my original question, and found a satisfying piece of math to go along with it!

这样,我想我已经彻底涵盖了我原来的问题,并且发现了令人满意的数学方法!

翻译自: https://medium.com/@pfnafjb/the-problem-with-palindromes-f81cfa3ffcb3

回文数怎么计算

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值