projecteuler---->problem=14----Longest Collatz sequence

时间:2022-09-04 09:28:55

title:

The following iterative sequence is defined for the set of positive integers:

n projecteuler---->problem=14----Longest Collatz sequence
n/2 (n is even)

n projecteuler---->problem=14----Longest Collatz sequence 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:

13 projecteuler---->problem=14----Longest Collatz sequence 40
projecteuler---->problem=14----Longest Collatz sequence 20
projecteuler---->problem=14----Longest Collatz sequence 10
projecteuler---->problem=14----Longest Collatz sequence 5
projecteuler---->problem=14----Longest Collatz sequence 16
projecteuler---->problem=14----Longest Collatz sequence 8
projecteuler---->problem=14----Longest Collatz sequence 4
projecteuler---->problem=14----Longest Collatz sequence 2
projecteuler---->problem=14----Longest Collatz sequence 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. String,StringBuffer与StringBuilder的区别??

    转自http://blog.csdn.net/rmn190/article/details/1492013 String 字符串常量 StringBuffer 字符串变量(线程安全) StringBu ...

  2. ubuntu中禁用华硕S550C触摸屏的方法

    华硕S550C的触摸屏被我一不小心弄了一条裂缝,导致屏幕一直会莫名其妙自动进行点击,严重影响了使用.在windows 系统下通过FN+F7的快捷键可以直接禁用触摸屏,但是换成ubuntu 系统之后,快 ...

  3. 获取Enum枚举值描述的几法方法

    原文:获取Enum枚举值描述的几法方法 1.定义枚举时直接用中文 由于VS对中文支持的很不错,所以很多程序员都采用了此方案. 缺点:1.不适合多语言 2.感觉不太完美,毕竟大部分程序员大部分代码都使用 ...

  4. logstash安装配置

    vim /usr/local/logstash/etc/hello_search.conf 输入下面: input { stdin { type => "human" }} ...

  5. 自定义Func模块

    自定义Func模块 (1)自定义模块步骤 (2)生成模块 [root@controller modules]# cd /usr/lib/python2.7/site-packages/func/min ...

  6. Nginx 的安装与配置

    一.下载 Linux:CentOS 7.3 64位 Nginx:nginx-1.13.1 安装目录:/usr/local/ wget http://nginx.org/download/nginx-1 ...

  7. Java技术开发程序员如果在2019年立足

    2019年的互联网环境相对以往来说要更复杂一些,互联网领域也正在经历从消费互联网向产业互联网转型的阵痛期.其实不少公司从2018年开始已经在陆续进行结构化调整,这些调整中的重要内容就是岗位调整,而岗位 ...

  8. scrapy爬取58同城二手房问题与对策

    测试环境: win10,单机爬取,scrapy1.5.0,python3.6.4,mongodb,Robo 3T 其他准备: 代理池:测试环境就没有用搭建的flask抓代理,因为我找到的几个免费网站有 ...

  9. 20181013xlVba年级成绩报表

    Public Sub 高一成绩报表() Application.ScreenUpdating = False Application.DisplayAlerts = False Application ...

  10. pheanstalk put 延时队列

    用pheanstalk客户端投放延时任务时,按照文档的参数顺序投放起不到延时的效果,取出(预订)job时data获取的数据也不是投放的字段值, put <pri> <delay&gt ...