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 ...
随机推荐
- 24位和8位BMP图片保存纯C代码
BMP图片大家都知道,可以通过查看BMP图片结构使用纯C就可以打开,编辑,处理,保存图片.非常方便使用. 具体BMP结构可以参考:wingdi.h头文件.今天主要在进行删减代码,需要把多余的代码删除, ...
- [Android学习笔记]view的layout过程学习
View从创建到显示到屏幕需要经历几个过程: measure -> layout -> draw measure过程:计算view所占屏幕大小layout过程:设置view在屏幕的位置dr ...
- IOS学习之路二十(程序json转换数据的中文字符问题解决)
ios请求web中的json数据的时候经常出现乱码问题: 例如请求结果可能如下:"\U00e5\U00a5\U00bd\U00e8\U00ae\U00a4" 在网上查到的解决方法是 ...
- 我经历的IT公司面试及离职感受(转)
毕业后几年一直待在广州,觉得这是一个比较生活化及务实的城市,其互联网公司和相应的投融资环境都不如北深上活跃,大大小小的面试也有几十个,有点规模的公司应该都面试过了,面试一般会见到主力技术人员,技术主管 ...
- Doxgen+Graphiz+htmlhelp配置
查看一些开源码常常被一些函数的调用关系给绕进去.找个工具生成个调用关系图或简单的文档对于帮助阅读程序有非常大的帮助. 1 doxgen+graphviz+htmlhelp简单介绍 1.1 doxgen ...
- Wamp环境下配置--Apache虚拟主机
1.首先打开apache的配置文件httpd.conf,并去掉#Include conf/extra/httpd-vhosts.conf前面的#,启用虚拟主机功能 # Virtual hosts In ...
- undefined reference to `sin'问题解决
作者:zhanhailiang 日期:2014-10-25 使用gcc编译例如以下代码时报"undefined reference to `sin'": #include < ...
- vs2008+opencv2.4.9 +win7X64位系统 2.
小编用自身血淋淋的例子,来给大家做个参考,共耗时近2天时间,终于屈服于安装vs2010,然后配置成功了.但是在这个配置成功后,我终于发现了我08配置不成功的原因,写下心得,供各位参考 1.准备工具 v ...
- IL来理解属性
IL来理解属性 阅读目录 概述: C#中如何定义一个属性 Student类 属性Name Main方法 实现get,set方法 性能 访问权限 回到最开始提出的问题 参考资料 .Net底层剖析目录 ...
- Windows Phone开发(37):动画之ColorAnimation
原文:Windows Phone开发(37):动画之ColorAnimation 上一节中我们讨论了用double值进行动画处理,我们知道动画是有很多种的,今天,我向大家继续介绍一个动画类--Colo ...