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:

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.

翻译:

以下的循环数列是由正整数依据以下规则构成的:

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

  1. Project Euler 14 Longest Collatz sequence

    题意:对于任意一个数 N ,寻找在 100,0000 之内按照规则( N 为奇数 N = N * 3 + 1 ,N 为偶数 N = N / 2 ,直到 N = 1 时的步数 )步数的最大值 思路:记忆 ...

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

  3. Problem 14

    Problem 14 # Problem_14.py """ The following iterative sequence is defined for the se ...

  4. 【LeetCode OJ】Longest Consecutive Sequence

    Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classi ...

  5. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  6. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

  8. 128. Longest Consecutive Sequence(leetcode)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. ACM Longest Repeated Sequence

    Description You are given a sequence of integers, A = a1, a2, ... an. A consecutive subsequence of A ...

  10. [LintCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. H ...

随机推荐

  1. touch修改文件的修改时间和访问时间,ls --full-time显示文件详细,stat命令

    1. 同时修改文件的修改时间和访问时间 touch -d "2010-05-31 08:10:30" test.doc 2. 只修改文件的修改时间 touch -m -d &quo ...

  2. dsp下基于双循环缓冲队列的视频采集和显示记录

    对最近在设计的视频采集和显示缓冲机制做一个记录,以便以后使用. 视频采集和显示缓冲机制,其实是参考了Linux下v4L2的驱动机制,其采用输入多缓冲frame,输出多缓冲的切换机制.简单的就是ping ...

  3. Java Word Ladder(字梯)

    问题: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...

  4. java中线程中的相关知识点

    (1)守护线程必须在线程start前设置(2)守护线程在所有用户线程结束后,也会终止(3)由于(2)所有守护线程不能执行一些读写操作,原因:如果守护线程在执行读写操作时,如果用户线程结束了,守护线程的 ...

  5. oschina 手机/移动开发

    手机/移动开发 Android UI 组件(167) React Native 相关(8) 网站客户端(16) NativeScript 插件(18) iPhone/iPad开发工具(16) WP7开 ...

  6. C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名

    原文:C# 文件操作(全部) 追加.拷贝.删除.移动文件.创建目录 修改文件名.文件夹名 本文也收集了目前最为常用的C#经典操作文件的方法,具体内容如下:C#追加.拷贝.删除.移动文件.创建目录.递归 ...

  7. 西南民大oj(矩阵快速幂)

    我的名字不可能那么难记 时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte总提交 : 16            测试通过 : ...

  8. Linux iptables简单配置

    #!/bin/sh#modprobe ipt_MASQUERADEmodprobe ip_conntrack_ftpmodprobe ip_nat_ftpiptables -Fiptables -t ...

  9. 恢复gvim的ctl+v可视模式设置

    set nocompatiblesource $VIMRUNTIME/vimrc_example.vim"source $VIMRUNTIME/mswin.vim (注释此行)behave ...

  10. 逆向 Framework.jar

    Ref:http://www.addictivetips.com/mobile/what-is-odex-and-deodex-in-android-complete-guide/ Ref:http: ...