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. [C++]new和delete

    Date:2014-1-5 Summary: C++中的动态内存创建与释放(这里就只记录C++中的new和delete了,其他的C风格操作就略过了) 单独记录new和delete的原因是为了学习时候关 ...

  2. JAVA必备——13个核心规范

    标准的价值: 你听过这句话吗?"一流企业做标准.二流企业做品牌.三流企业做产品!"我时我就在想,做标准的企业就是一流的?卖产品就是三流公司?而坐产品或者加工的公司,即使说销售量非常 ...

  3. HDU 3571 N-dimensional Sphere(高斯消元 数论题)

    这道题算是比较综合的了,要用到扩展欧几里得,乘法二分,高斯消元. 看了题解才做出来orz 基本思路是这样,建一个n*(n-1)的行列式,然后高斯消元. 关键就是在建行列式时会暴long long,所以 ...

  4. Android中使用JNI获得APK签名的哈希值

    原地址:http://blog.csdn.net/i5suoi/article/details/19036975 最近在研究android应用中的安全问题,貌似只有将核心代码写到JNI底层才是最安全的 ...

  5. HDU 3613 Best Reward 正反两次扩展KMP

    题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...

  6. android视频库Vitamio

    之前尝试自己解码视频,然后播放显示,虽然音视频都可以播放,但是实现不了音视频的同步,所以使用第三方的视频库Vitamio来实现视频播放器功能,这样自己只需要实现播放解码的制作不不要关心底层解码和显示问 ...

  7. hdu4223(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4223 由于n范围较小,完全可暴力... #include <cstdio> #includ ...

  8. Android利用反射获取状态栏(StatusBar)高度

    MainActivity如下: package cc.teststatusbarheight; import java.lang.reflect.Field; import android.os.Bu ...

  9. sql大小转换函数

    将字段值转换成大写 UPDATE t SET [name]=UPPER([name]) 将字段值转换成小写 UPDATE t SET [name]=LOWER([name])

  10. Cordova CLI源码分析(二)——package.json

    每个包需要在其顶层目录下包含一个package.json文件,该文件不仅是包的说明,也影响npm安装包时的配置选项 更多参数详见参考文档https://npmjs.org/doc/json.html ...