projecteuler---->problem=14----Longest Collatz sequence
title:
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:









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.
翻译:
以下的循环数列是由正整数依据以下规则构成的:
n →
n/2 (若n是偶数)
n → 3n + 1 (若n是奇数)
若数列从13開始,就生成了例如以下数列:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
显然以上数列有10个数字,尽管未经证明(著名的Collatz猜想),但我们觉得不管由什么数字開始。数列都会在1处结束。故数列一旦产生了1这一项,就觉得数列结束。
这次的问题是:依据以上规则。由100万下面的哪个数字開始。能够产生最长的数列?
请注意:产生的数列可能会包括数字超过100万的项。
import time
def f(n):
if n%2==1 and n>1:
return f(3*n+1)+1
elif n%2==0:
return f(n/2)+1
return 1
m,value=0,0
begin=time.time()
for i in range(1,1000000):
tmp=f(i)
if tmp>m:
value=i
m=tmp
print time.time()-begin
print m,value
版权声明:本文博客原创文章。博客,未经同意,不得转载。
projecteuler---->problem=14----Longest Collatz sequence的更多相关文章
- Project Euler 14 Longest Collatz sequence
题意:对于任意一个数 N ,寻找在 100,0000 之内按照规则( N 为奇数 N = N * 3 + 1 ,N 为偶数 N = N / 2 ,直到 N = 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 ...
- Problem 14
Problem 14 # Problem_14.py """ The following iterative sequence is defined for the se ...
- 【LeetCode OJ】Longest Consecutive Sequence
Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
- 128. Longest Consecutive Sequence(leetcode)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- ACM Longest Repeated Sequence
Description You are given a sequence of integers, A = a1, a2, ... an. A consecutive subsequence of A ...
- [LintCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...
随机推荐
- Loser tree in Python | Christan Christens
Loser tree in Python | Christan Christens Loser tree in Python I am taking an Advanced Data Structur ...
- [置顶] 页面缓存,cache,设置缓存过期时间,OutputCache
页面缓存 方法一: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //缓存有数据 if (Cach ...
- 一致性哈希算法(consistent hashing)样例+測试。
一个简单的consistent hashing的样例,非常easy理解. 首先有一个设备类,定义了机器名和ip: public class Cache { public String name; pu ...
- Android内存管理
首先Android理机制相当复杂.想要讲清楚比較困难.其次对于绝大多数用户来说.仅仅关心内存够不够用,至于内存怎样管理的这样的技术细节,不是用户须要去考虑的,写这样一个专题有没有意义?毕竟我们是用手机 ...
- Hadoop-2.2.0中国文献—— MapReduce 下一代 -- 公平调度
目的 此文档描写叙述了 FairScheduler, Hadoop 的一个可插入式的调度器.同意 YARN 应用在一个大集群中公平地共享资源. 简单介绍 公平调度是一种分配资源给应用的方法,以致到最后 ...
- iPhone、iPad强制关机
情景:iPad測试应用过程中死机了. 解决:同一时候按住右上方的电源键和屏幕下方的HOME键大约10秒左右. 就会自己主动强制断电关机,然后重新启动. 强制重新启动后你会看到进入苹果的标志,然后进入主 ...
- 西南民大oj(两园交求面积)
西南民大oj:http://www.swunacm.com/acmhome/welcome.do?method=index 我的几何不可能那么可爱 时间限制(普通/Java) : 1000 MS/ 3 ...
- 服务器编程入门(1)TCP/IP协议族
问题聚焦: 简单地梳理一下TCP/IP各层的功能和常用协议 详细了解ARP(数据链路层)和DNS(应用层)协议的工作原理 1 TCP/IP协议族体系结构 数据链路层: 职责:实现网卡接口的网络 ...
- ERROR: Error in Log_event::read_log_event(): 'read error', data_len: 438, event_type: 2
分析从库1062问题,解析从库binlog日志,报错如下 [root@xxxdb0402 tmp]# mysqlbinlog mysql-bin.004271 > 4.log ERROR: Er ...
- dsbskrhkme看么哦么
http://pan.baidu.com/share/link?shareid=3011665141&uk=338692646&third=15 http ...