[英]Migrating Excel financial model to and Corkscrew calculation in Python Pandas
[英]Python: Corkscrew method to define a n - parastic number
分配:编写一个带有两个自然数n和k的函数开瓶器。 该函数必须返回以应用开瓶器方法产生的数字k结束的n寄生数。 该函数可以假设n和k均为一位自然数,且k≥n。
例:
corkscrew (5, 7)
5 x 7 = 35
5 x 57 = 285
5 x 857 = 4285
5 x 2857 = 14285
5 x 42857 = 214285
5 x 142857 = 714285
问题:在for循环中使用我的代码,该数字不变,因此结果res不会改变或演变为n-寄生数。
def parasitic(number):
"""
>>> parasitic(179487)
4
>>> parasitic(142857)
5
>>> parasitic(105263157894736842)
2
>>> parasitic(1234)
0
"""
for n in range(2, 11):
res = n * number
if str(number)[-1] + str(number)[:-1] == str(res):
return n
return 0
def corkscrew(n, number):
"""
>>> corkscrew(4, 7)
179487
>>> corkscrew(5, 7)
142857
>>> corkscrew(2, 2)
105263157894736842
"""
res = n * number
count = 0
for i in range(1, n):
res = n * (str(res)[-i:] + str(number))
count += 1
if parasitic(number) == n:
return rotateLeft(res)
def rotateLeft(number):
"""
>>> rotateLeft(717948)
179487
>>> rotateLeft(142857)
428571
>>> rotateLeft(105263157894736842)
52631578947368421
"""
k = str(number)
letter = k[:1]
numb = k[1:]
resultaat = str(numb) + str(letter)
return int(resultaat)
回答有效的问题:
def corkscrew(n, number):
"""
>>> corkscrew(4, 7)
179487
>>> corkscrew(5, 7)
142857
>>> corkscrew(2, 2)
105263157894736842
"""
res = n * number
count = 0
for i in range(0, 65):
numb = (str(res)[-i:] + str(number))
numb = int(numb)
res = n * numb
count += 1
if parasitic(numb) == n:
return numb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.