如何更快地使此函数:算法基于偶数和奇数创建链
在Python中,我想编写一个函数,该函数返回一个数字集中的数字集,该数字集导致基于计算算法创建的最长序列(“ 如果n甚至除以两个,则乘以两个乘以三个,然后添加一个“)。数字3的Fe,序列将为 3-> 10 - > 5-> 16-> 8-> 4-> 2 - > 1 。
我尝试编写快速,非幼稚的函数:
def my_fun(n):
dici = {k: v for k, v in zip(range(n), range(n))}
count = 0
while (len(dici)>1):
count += 1
dici = {k:(v/2 if v%2 == 0 else v*3+1) for k,v in {key: dici[key] for key in dici if dici[key] > 1}.items() }
while all(value != 1 for value in dici.values()):
dici = {k:(v/2) for k, v in dici.items()}
count +=1
return dici, count
输入10,该功能返回9作为导致最长链的数字(长度19)。
正如我想将功能应用于更多的数字时,此版本在计算上太广泛了。有什么方法可以缩短我的代码?
in Python I would like to write a function that returns the number from a number set that leads to the longest sequence created based on a calculation algorithm ("if n is even divide by two else multiply by three and add one"). F.e. for the number 3, the sequence would be 3-> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1.
My attempt of writing a fast, non naive function:
def my_fun(n):
dici = {k: v for k, v in zip(range(n), range(n))}
count = 0
while (len(dici)>1):
count += 1
dici = {k:(v/2 if v%2 == 0 else v*3+1) for k,v in {key: dici[key] for key in dici if dici[key] > 1}.items() }
while all(value != 1 for value in dici.values()):
dici = {k:(v/2) for k, v in dici.items()}
count +=1
return dici, count
With the input of 10, the function returns 9 as the number leading to the longest chain (length 19).
As I would like to apply my function to greater numbers, this version is way too computationally extensive. Is there any way to shorten my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
//
(有一个非终止循环的7)
n // 2 ,那里一步是大两倍的两倍:
从
//
(got a non-ending loop for 7)
n//2
, there is one twice as large taking one step more:Start with