当前位置:网站首页>检测循环数“142857“
检测循环数“142857“
2022-06-13 06:56:00 【飞鸟真人】
描述
若一个n位的数字串满足下述条件,则称其是循环数(cyclic):将这个数字串视为整数(可能带有前导0),并用任意一个 1 到 n 之间(包含1和n)的整数去乘它时, 会得到一个将原数字串首尾相接后,再在某处断开而得到的新数字串所对应的整数。例如,数字 142857 是循环数,因为:
142857 *1 = 142857
142857 *2 = 285714
142857 *3 = 428571
142857 *4 = 571428
142857 *5 = 714285
142857 *6 = 857142。
请写一个程序判断给定的数是否是循环数。
注意:在此题中,输入数字串允许带前导0,且前导0不能被忽略,例如“01”是两位数字串,而“1”是一位数字串。但将数字串转化为整数做乘法运算或比较运算时,可以忽略前导0。
python的好处是支持大数计算,如果是C++则需要自己写函数做大数加减乘除;
思路:
1)写一个函数验证,是否两个数经过移位是相同的;这里使用暴力循环比较;
2)写一个函数做2 ~ len(s) 的乘法,每个乘法结果都转为字符串,并用0前缀补齐长度;比较是否经过移位后相同;
# 0 <= n <= len(s)
def numShift(s, n):
d = s[n:] + s[:n]
return d
# check string is same with s1,
def checkSame(s, s1):
#print("-----------------check same----------------")
for i in range(1, len(s)):
d = numShift(s, i)
#print("%d, %s" % (i, d))
if (s1 == d):
return True
return False
# check cycle num with multi by [2... len(num)]
def checkNum(num):
for i in range(2, len(num)+1):
newNum = int(num) * i
num1 = str(newNum).zfill(len(num)) # 前面补全0
print("%s * %d = %s" % (num, i, num1) )
if False == checkSame(num, num1):
return False
return True
b = checkNum("253968")
#b = checkNum("142857")
#b = checkSame("142857", "714285")
print(b)
边栏推荐
- 【转】FPGA面试题
- 我的理财产品显示清算中是什么意思?
- 10 Honest Facts I Want To Share With All Junior Developers
- Chain 2+1 reward, what kind of mode is beautiful everyday seconds?
- 怎么写出一份令人惊叹的设计文档?
- [weak transient signal detection] matlab simulation of SVM detection method for weak transient signal under chaotic background
- JS method of extracting numbers from strings
- Why is the blind box e-commerce mode so popular?
- When the concept of industrial Internet just appeared, people only regarded it as a method to obtain b-end traffic
- Host computer development (Architecture Design of firmware download software)
猜你喜欢
Differences between SQL and NoSQL of mongodb series
2022-06-12:在N*N的正方形棋盤中,有N*N個棋子,那麼每個格子正好可以擁有一個棋子。 但是現在有些棋子聚集到一個格子上了,比如: 2 0 3 0 1 0 3 0 0 如上的二維數組代錶,一
上位机开发(固件下载软件之详细设计)
Jinglianwen Technology: current situation and solutions of data annotation industry
Jfinal uses freemaker to output map.
As the new trend of blind box e-commerce, how can the platform use blind box play to drain at low cost?
Jinglianwen Technology: current situation and solutions of data acquisition and labeling industry
智能金融再升级,景联文科技提供数据采集标注服务
Ansible PlayBook的中清单变量优先级分析及清单变量如何分离总结
Byte (nine)
随机推荐
十五、IO流(一)
Intelligent entertainment has developed steadily, and jinglianwen technology provides data collection and labeling services
How to use Wangyou DFM software for cold plate analysis
杭州证券开户是安全的吗?
玄武云科技通过上市聆讯:业绩波动明显,陈永辉等三人为控股股东
Jinglianwen technology provides voice data acquisition and labeling services
What is the essence of social e-commerce disruption? How can businesses get more traffic?
树莓派高级开发——“IO口驱动代码的编写“ 包含总线地址、物理_虚拟地址、BCM2835芯片手册知识
package-lock. json
【微弱瞬态信号检测】混沌背景下微弱瞬态信号的SVM检测方法的matlab仿真
How to seize the bonus of social e-commerce through brand play to achieve growth and profit?
Br tool backup recovery
杭州网上开户是安全的吗?
Jinglianwen Technology: current situation and solutions of data acquisition and labeling industry
Outil de formatage du temps - mode. JS (affichage en temps réel du temps Web)
线程池中的 工作线程如何被回收
[turn to] FPGA interview questions
ML之FE:Vintage曲线/Vintage分析的简介、计算逻辑、案例应用之详细攻略
【RS-422与RS-485】RS-422与RS-485串行接口标准
我的理财产品显示清算中是什么意思?