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的更多相关文章

  1. (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 ...

  2. uoj problem 14 DZY Loves Graph

    题目: DZY开始有 \(n\) 个点,现在他对这 \(n\) 个点进行了 \(m\) 次操作,对于第 \(i\) 个操作(从 \(1\) 开始编号)有可能的三种情况: Add a b: 表示在 \( ...

  3. 【UOJ #14】【UER #1】DZY Loves Graph

    http://uoj.ac/problem/14 题解很好的~ 不带路径压缩的并查集能保留树的原本形态. 按秩合并并查集可以不用路径压缩,但是因为此题要删除,如果把深度当为秩的话不好更新秩的值,所以把 ...

  4. Codeforces Round #14 D. Two Paths(求树上两条不相交的路径的乘积最大值)

    题目链接:  http://codeforces.com/problemset/problem/14/D 思路:直接枚举每一天路径的两端,然后求以每一端为树根的树上最长路径,然后相乘就可以了. # ...

  5. uoj #14.【UER #1】DZY Loves Graph

    http://uoj.ac/problem/14 由于加入的边权递增,可以直接运行kruskal并支持撤销,但这样如果反复批量删边和撤销,时间复杂度会退化,因此需要对删边操作加上延时处理,只有在删边后 ...

  6. Common Bugs in C Programming

    There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...

  7. Error with mysqld_safe

    出处:http://bugs.mysql.com/bug.php?id=18403 Description: - I downloaded the binary file “Standard 5.0. ...

  8. codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]

    就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...

  9. Object Tracking Benchmark

    Abstract 问题: 1)evaluation is often not suffcient 2)biased for certain types of algorthms 3)datasets ...

随机推荐

  1. oralce sql 创建指定时间段内的日历信息

    -- Create table create table TEMP_CALENDAR (   MONTH VARCHAR2(6),   W_7   VARCHAR2(2),   W_1   VARCH ...

  2. 何时使用static类(工具类)

    一个static类,指所有成员都是static方法构成的.而没有不论什么成员变量, 也称为Utility class 或者Utility Pattern [參考: Utility Pattern].它 ...

  3. java 10000的阶乘

    package test; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import ...

  4. 每天一个linux命令(01):ifconfig命令

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  5. 框架:Rureka

    ylbtech-框架:Rureka Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的.S ...

  6. [HTML5] 新标签解释及用法

    转自:http://www.cnblogs.com/yuzhongwusan/archive/2011/11/17/2252208.html HTML 5 是一个新的网络标准,目标在于取代现有的 HT ...

  7. css兼容性的一些问题

    1. 文字本身的大小不兼容.同样是font-size:14px的宋体文字,在不同浏览器下占的空间是不一样的,ie下实际占高16px,下留白3px,ff 下实际占高17px,上留白1px,下留白3px, ...

  8. 关于作者&情况

    本校第一次做信奥 , 如有错误, 见谅 本人之前从未接触编程, 选择信奥也只是因为怕被其他奥赛给淘汰... 这应该是懦弱吧...... 但自从接触编程以来, 虽然算不上极大的热爱, 但发自内心地喜欢它 ...

  9. 如何用SVG写一个环形进度条以及动画

    本次案例主要使用了svg的三个元素,分别为circle.text.path,关于svg的介绍大家可以看MDN上的相关教程,传送门 由于svg可以写到HTML中,所以这里我们就可以很方便的做进度条加载动 ...

  10. c.vim的安装和使用

    之前写了一些废话,今天写的稍微具体点. 在mac上给vim装个插件,,倒腾了半天 先要去找到c.vim.zip,自己百度下,官网就有的,我mac下下来,是直接解压的格式,而不是*.zip,也就是说不需 ...