1.前言
最近在学习Python教程,学习到关于回文数(palindrome number)的算法, 于是想写一篇关于回文数的博客,放在Math专栏,当做一个知识点,也是作为积累的一步吧。
2.回文数
什么是回文数?
A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed—by wikipedia
翻译为:一个回文数或者数字的“回文”就是一个数经过颠倒仍保持不变。这是通俗解释。回文数多用于趣味数学教材中。
Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number n > 0 in base b ≥ 2, where it is written in standard notation with k+1 digits ai as:
∑ki=0aibi
with, as usual,
0≤ai<b
for all i and
ak≠0
. Then
n
is palindromic if and only if
翻译为:尽管回文数多用于十进制数中,回文的性质可以用于任意进制的自然数。考虑数
∑ki=0aibi
通常,对于所有
i
来说,
把所有的系数写成一个tuple(tuple是学python时遇到的,表示元组常量):
这个tuple的首尾值相等,即
ai
=
ak−i
3、Python算法实现打印1~200内的回文数
def is_palindrome(n):
num=n
res=0
while num>0:
res=res*10+num%10
num=int(num/10)
if res==n:
return n
print(list(filter(is_palindrome,list(range(1,200)))))
4.进位制
http://www.matrix67.com/blog/archives/202
(1)、感谢此文博主讲解的进位制。而在回文数中默认采用一般的进位制,即不采用负进位制,所以负数就不是回文数。
(2)、特殊的进位制–1进位制
1进位制包含数0?此处我觉得应该特殊对待1进制
0—表示1进位制的1=十进位制的1
00–表示1进位制的2=十进位制的2
这个0不是必须的,可以用划竖线表示,或者划横线等等
依次下去。至于1进制的实际0用什么表示?可以用空表示,什么都没有。记得有个故事讲的就是猫
A
记得猫