Project Euler: Problem 14 Longest Collatz sequence
来源:互联网 发布:ch341a编程器软件1.30 编辑:程序博客网 时间:2024/05/17 06:52
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.
用了一个傻逼的方法,尽管答案对了,但跑起来非常慢
#include <iostream>#include <string>using namespace std;long long count(long long a){int res = 0;while (a != 1){res++;if (a % 2 == 0)a = a / 2;elsea = 3 * a + 1;}res++;return res;}int main(){long long max = 0;long long res;for (int i = 1000000; i >= 1; i--){//cout << i << endl;long long tp = count(i);if (max < tp){res = i;max = tp;}}cout << res << endl;system("pause");return 0;}
0 0
- Project Euler: Problem 14 Longest Collatz sequence
- Project Euler Problem 14 Longest Collatz sequence
- 14 Longest Collatz sequence - Project Euler
- project euler 14 Longest Collatz sequence
- Project Euler 014 Longest Collatz sequence
- Problem 14:Longest Collatz sequence
- Problem 14:Longest Collatz sequence
- (Problem 14)Longest Collatz sequence
- projecteuler---->problem=14----Longest Collatz sequence
- Problem 14 Longest Collatz sequence(暴力)
- PE 14 Longest Collatz sequence
- Longest Collatz sequence
- Longest Collatz sequence
- Project Euler Problem 14
- Project Euler problem 14
- project euler problem 14
- Project Euler – Problem 14
- 欧拉计划(14)Longest Collatz sequence
- iOS的Plist-XML-JSON数据解析
- Git clone远程分支
- Linux Shell脚本攻略(1.8)
- ZigBee入门之基础概念3
- MVC北京络捷斯特第三方物流系统技术解析(六)补录订单运输信息
- Project Euler: Problem 14 Longest Collatz sequence
- 图解WebSphere 8.5 安装
- WCF 之 服务契约(Service Contract)
- dmytrodanylyk/circular-progress-button源码解析(一)
- Spring AOP--引入增强
- Gson-JsonParser解析Json字符串
- 从数据库看学习
- openlayers实现wfs属性查询和空间查询
- git步步进阶---实战git的windows安装和使用