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:
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.
翻译:
以下的循环数列是由正整数依据以下规则构成的:
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 ...
随机推荐
- [Windows Phone]修改应用程序主题
问题: Windows Phone模拟器默认情况下是黑色(Dark)主题,开发者往往都是在黑色主题下进行应用开发,加入自定义的颜色,样式等等,而当把手机操作系统主题设为白色(Light)主题之后会发现 ...
- Enthought科学计算,数据分析
Enthought Canopy: Easy Python Deployment Plus Integrated Analysis Environment for Scientific Computi ...
- CS0433: 类型“BasePage”同一时候存在于“c:\Windows\Microsoft.NETxxxxxxxxxxxxxxxx
网上常见的我就不说了. 假设其他地址的方法解决不了你的问题,那么请往下看. 该类是否存放于 App_Code 下,假设是把该类从App_Code中拉出来,然后再次执行试试.
- Vim -> 边确认边查找替换
进行简单的全局查找替换的时候,能够使用ex的例如以下命令 :%s/which/what/g 它的使用方法比較简单 % 表示每一行(everyline) s 替换(substitue) which 查找 ...
- Cocos2d-x 3.1.1 Lua实例-AccelerometerTest(重力加速计)
Cocos2d-x 3.1.1 Lua实例-AccelerometerTest(重力加速计) 本篇博客介绍Cocos2d-x的第一个实例--重力加速计測试.效果图(注:这里无法模拟重力感应): --[ ...
- 基于Servlet、JSP、JDBC、MySQL的一个简单的用户注冊模块(附完整源代码)
近期看老罗视频,做了一个简单的用户注冊系统.用户通过网页(JSP)输入用户名.真名和password,Servlet接收后通过JDBC将信息保存到MySQL中.尽管是个简单的不能再简单的东西,但麻雀虽 ...
- SimpleDateFormat 的线程安全问题与解决方式
SimpleDateFormat 的线程安全问题 SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的详细类. 它同意格式化 (date -> text).语法分析 (te ...
- 只能从脚本中调用在类定义上有[ScriptService]属性的Web服务问题的解决方案
ajax调用webservice中的接口时, 会出现[只能从脚本中调用在类定义上有[ScriptService]属性的...]的异常. 这是因为, 在.net3.5中, 访问web服务, 要对web服 ...
- jdk1.6与1.7垃圾回收
最近项目中遇到了个关于JVM中GC线程数的问题,做一下简单的总结 问题场景: server:均为 sun公司的solaris 系统 CPU 128个 项目8.1时使用的 java版本: jdk1. ...
- 编译v8引擎
本机环境是win7+vs2010(本来想用2012的,但是发现默认的项目是2010的) 1.首先下载V8的源码 安装好svn,并在cmd下能使用svn help: svn下载地址(安装包):http: ...