Problem 14
Problem 14
# Problem_14.py
"""
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.
""" def is_even_or_odd(num):
if num % 2 == 0:
return 'even'
else:
return 'odd' def chain(num):
li = []
li.append(num)
while True:
if num != 1:
if is_even_or_odd(num) == 'even':
num //= 2
li.append(num)
else:
num *= 3
num += 1
li.append(num)
else:
break
return li if __name__ == '__main__':
max_chain_length = 0
max_chain_starts = 0
for i in range(1, 1000001):
li = chain(i)
if len(li) > max_chain_length:
max_chain_length = len(li)
max_chain_starts = i
print(i, len(li))
print(max_chain_starts, max_chain_length)
Problem 14的更多相关文章
- (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 ...
- uoj problem 14 DZY Loves Graph
题目: DZY开始有 \(n\) 个点,现在他对这 \(n\) 个点进行了 \(m\) 次操作,对于第 \(i\) 个操作(从 \(1\) 开始编号)有可能的三种情况: Add a b: 表示在 \( ...
- 【UOJ #14】【UER #1】DZY Loves Graph
http://uoj.ac/problem/14 题解很好的~ 不带路径压缩的并查集能保留树的原本形态. 按秩合并并查集可以不用路径压缩,但是因为此题要删除,如果把深度当为秩的话不好更新秩的值,所以把 ...
- Codeforces Round #14 D. Two Paths(求树上两条不相交的路径的乘积最大值)
题目链接: http://codeforces.com/problemset/problem/14/D 思路:直接枚举每一天路径的两端,然后求以每一端为树根的树上最长路径,然后相乘就可以了. # ...
- uoj #14.【UER #1】DZY Loves Graph
http://uoj.ac/problem/14 由于加入的边权递增,可以直接运行kruskal并支持撤销,但这样如果反复批量删边和撤销,时间复杂度会退化,因此需要对删边操作加上延时处理,只有在删边后 ...
- Common Bugs in C Programming
There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...
- Error with mysqld_safe
出处:http://bugs.mysql.com/bug.php?id=18403 Description: - I downloaded the binary file “Standard 5.0. ...
- codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]
就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...
- Object Tracking Benchmark
Abstract 问题: 1)evaluation is often not suffcient 2)biased for certain types of algorthms 3)datasets ...
随机推荐
- error at ::0 can't find referenced pointcut...解决方法
error at ::0 can't find referenced pointcut...解决方法 学习了:http://dyldragon.iteye.com/blog/512612 升级aspe ...
- linux 线程切换效率与进程切换效率相差究竟有多大?
Author:DriverMonkey Mail:bookworepeng@Hotmail.com Phone:13410905075 QQ:196568501 Are Linux threads t ...
- Could not read from remote repository.
今天换新电脑,忘了配置git环境,就去gitserver上代替码.然后一直报错,后来就又一次配置了git环境.步骤例如以下 damingwuage:Desktop damingwuage$ ssh-k ...
- BZOJ 4491 分块OR差分+线段树
思路: (是不是只有我作大死写了个分块) up[i][j]表示从第i块开始到第j个位置 上升的最大值 down[i][j]同理 left_up[i]表示从第i块开始能够上升的最长长度 left_dow ...
- java中的访问修饰符2
综上所述:protected强调的是子类,deafult强调的是本包,private强调的是本类,public强调的是开放性.
- Hadoop MapReduce编程 API入门系列之统计学生成绩版本2(十八)
不多说,直接上代码. 统计出每个年龄段的 男.女 学生的最高分 这里,为了空格符的差错,直接,我们有时候,像如下这样的来排数据. 代码 package zhouls.bigdata.myMapRedu ...
- Hadoop MapReduce编程 API入门系列之wordcount版本2(六)
这篇博客,给大家,体会不一样的版本编程. 代码 package zhouls.bigdata.myMapReduce.wordcount4; import java.io.IOException; i ...
- react拼接class&将JS标签转换为HTML
1.在JS中混杂字符和HTML标签,识别方法: const menuList = ['门店', '星享俱乐部', '菜单', '<hr></hr>', '星巴克移动应用', ' ...
- javascript中创建对象和实现继承
# oo ##创建对象 1. 原型.构造函数.实例之间的关系 * 原型的construct->构造函数:调用isPrototypeOf(obj)方法可以判定和实例的关系: * 构造函数的pro ...
- windwo下载完nvm无法执行node
安装node版本管理工具之NVM.安装方法:见链接. window安装完后,下载node后,无法执行node.见图(图片从网上找的). 最后问题原因是,1.nvm安装时,安装目录中存在空格. 解决办法 ...