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 ...
随机推荐
- pdftk的使用介绍
首先像下面的一页pdf,如果想把它分成两页,每一页只是一个ppt页面(为了在kindle里读比较方便), 那么可以首先用A-pdf page cut, 将pdf 切成这样12个部分 然后我们现在要的只 ...
- 怎么获取Spring的ApplicationContext
在 WEB 开发中,可能会非常少须要显示的获得 ApplicationContext 来得到由 Spring 进行管理的某些 Bean, 今天我就遇到了,在这里和大家分享一下, WEB 开发中,怎么获 ...
- 【剑指offer】q34:丑数
题目要求第n个丑数.所以对于中间结果不须要保存. def Humble(index): curHum = 1 M2 = 2; M3 = 3; M5 = 5 while index > 1: cu ...
- atitit.高级编程语言的特性 and 未来趋势与进化.doc
atitit.高级编程语言的特性 and 未来趋势与进化.doc 1 编程语言的发展历程 1 1.1 编程语言的进化,起始发现背后的思想 :不论什么两个系统之间的复杂性,都能够通过加入一个抽象层要屏蔽 ...
- cxf和jboss eap 6.2版本号冲突
升级jboss版本号到jjboss-eap-6.2之后,启动项目时CXF出现异常. 在jboss-as-7.1.1.Final.apache-tomcat-7.0.37以及jboss-eap-6.1 ...
- poj3259(spfa判负环)
题目连接:http://poj.org/problem?id=3259 题意:John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时 ...
- js封装好的模仿qq消息弹窗代码
在我们的日常开发中,或者生活中.常常须要用到弹出窗.这里我们就用js模拟一下qq消息一样的弹出窗. 直接贴代码: <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- 获取webshell的十种方法
黑客在入侵企业网站时,通常要通过各种方式获取webshell从而获得企业网站的控制权,然后方便进行之后的入侵行为.本篇文章将如何获取webshell总结成为了十种方法,希望广大的企业网络管理员能够通过 ...
- CentOS 7 安装MySql Server 5.6
1. 安装MySql Server 在/etc/yum.repos.d/目录下添加以下文件mysql-community.repo文件,内容如下: [mysql56-community] name=M ...
- JarSearch
个人做的小工具分享给大家~~. 支持从压缩文件搜索文件,特别是根据部分类文件名在jar里查找文件,比较方便,效率也还不错. 也支持从目录查找 http://pan.baidu.com/s/1feYaM ...