本题来自 Project Euler 第14题:https://projecteuler.net/problem=14

'''
Project Euler: Problem 14: Longest Collatz sequence
The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even)
n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
It can be seen that this sequence (starting at 13 and finishing at 1)
contains 10 terms. Although it has not been proved yet (Collatz Problem),
it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million. Answer: 837799(共有525个步骤)
''' import time
startTime = time.clock() def f(x):
c = 0 #计算考拉兹序列的个数
while x != 1:
if x%2 == 0: #若为偶数
x = x//2
c += 1
else: #若为奇数
x = x*3+1
c += 1
if x == 1:
c += 1 #数字1也得算上
return c chainItemCount = 0
startingNumber = 0
for i in range(1, 1000000):
t = f(i)
if chainItemCount < t:
chainItemCount = t
startingNumber = i print('The number %s produces the longest chain with %s items' % (startingNumber, chainItemCount)) print('Time used: %.2d' % (time.clock()-startTime))

互动百科说了,考拉兹猜想--又称为3n+1猜想、角谷猜想、哈塞猜想、乌拉姆猜想或叙拉古猜想,是指对于每一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循环,最终都能够得到1。

判断条件很清楚,所以解题思路也很清晰:把 1-999999 之间的所有数字都拿来判断,计算每个数字分解到1所经历的步骤数,拥有最大步骤数的那个数字(Starting Number)即为解。

解这题,我的破电脑花了29秒。我隐约感觉这题应该有更好的解法,比如我很不喜欢的递归之类的……

Python练习题 042:Project Euler 014:最长的考拉兹序列的更多相关文章

  1. Python练习题 032:Project Euler 004:最大的回文积

    本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...

  2. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  3. Python练习题 046:Project Euler 019:每月1日是星期天

    本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...

  4. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  5. Python练习题 033:Project Euler 005:最小公倍数

    本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...

  6. Python练习题 049:Project Euler 022:姓名分值

    本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...

  7. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

  8. Python练习题 047:Project Euler 020:阶乘结果各数字之和

    本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...

  9. Python练习题 045:Project Euler 017:数字英文表达的字符数累加

    本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...

随机推荐

  1. Python测试函数运行时间

    import time import datetime # 测试函数运行时间 def cal_time(fn): """计算性能的修饰器""" ...

  2. Git使用感悟

    前言 分支介绍 我们现在开发的分支一般是这样的(基于上面那张图片的): master:上线用的 dev:开发用的 featature_xxx:开发用的 test:测试用的 hotfix:修复bug的 ...

  3. Cobalt strike与内网渗透

    cobalt strike的用法参照我之前的博客: https://www.cnblogs.com/vege/p/12743274.html 这里只演示上线之后的操作. Socks代理 开启socks ...

  4. python爬取B站视频弹幕分析并制作词云

    1.分析网页 视频地址: www.bilibili.com/video/BV19E… 本身博主同时也是一名up主,虽然已经断更好久了,但是不妨碍我爬取弹幕信息来分析呀. 这次我选取的是自己 唯一的爆款 ...

  5. 跟我一起学.NetCore之中间件(Middleware)简介和解析请求管道构建

    前言 中间件(Middleware)对于Asp.NetCore项目来说,不能说重要,而是不能缺少,因为Asp.NetCore的请求管道就是通过一系列的中间件组成的:在服务器接收到请求之后,请求会经过请 ...

  6. Photon PUN 一 介绍

    有句话说的好 , 官网永远是最好的学习地方 . 虽然国内的资料不多 , 但是官网的资料还是很充足 , 这就带着英汉词典就着作阅读理解的劲头去官网学习吧 https://doc.photonengine ...

  7. Mac上如何降级Java版本

    升级到了Java9,有些工具就不工作了.因此要降级到Java8.方法: /Library/Java/JavaVirtualMachines/下的高版本SDK即可

  8. 面向嵌入式的JavaScript引擎

    https://jerryscript.net/ https://duktape.org/ https://github.com/ialex32x/duktape-unity https://gith ...

  9. SpringCloud入门 消息总线 Bus

    消息总线 1.概述 使用SpringCloud Bus配和Spring Cloud Config使用实现配置的动态刷新 Bus只支持消息处理:RabbitMQ和Kafaka. 能干嘛 能管理和传播分布 ...

  10. node中间件

    npm i body-parser post 请求主题中间件 const bodyParser = require('body-parser')   const bodyParser = requir ...