(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 + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:









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开始以1结束的序列包含10个项。虽然还没有被证明(Collatz问题),但是人们认为在这个规则下,以任何数字开始都会以1结束。
以哪个不超过100万的数字开始,能给得到最长的序列? 注意: 一旦序列开始之后,也就是从第二项开始,项是可以超过100万的。
方法1:
- #include<stdio.h>
- #include<math.h>
- #include<stdbool.h>
- int powcount(long long n) //计算2的幂数
- {
- int count=;
- while(n>>=) count++;
- return count;
- }
- bool ispower(long long v) //判断n是否为2的幂
- {
- if(((v & (v - )) == )) return true;
- else return false;
- }
- int length(long long n)
- {
- int sum=;
- while()
- {
- if(n==) break;
- if((n & )==)
- {
- if(ispower(n)) return sum+powcount(n);
- else n=n/;
- }
- else n=*n+;
- sum++;
- }
- return sum;
- }
- int main()
- {
- int i,t,k,max=;
- for(i=; i<; i++)
- {
- t=length(i);
- if(t>max)
- {
- max=t;
- k=i;
- }
- }
- printf("%lld\n",k);
- return ;
- }
方法2:
- #include<stdio.h>
- #include<math.h>
- #include<stdbool.h>
- int a[];
- void find()
- {
- long long i,j,k,f,sum,max=;
- a[]=,a[]=;
- for(j=; j<; j++)
- {
- sum=,k=i=j;
- while()
- {
- if((i & )==)
- {
- i=i/;
- if(i<k)
- {
- a[k]=sum+a[i];
- break;
- }
- }
- else
- {
- i=*i+;
- }
- sum++;
- }
- if(a[k]>max)
- {
- max=a[k];
- f=k;
- }
- }
- printf("%d\n",f);
- }
- int main()
- {
- find();
- return ;
- }
Answer:
|
837799 |
(Problem 14)Longest Collatz sequence的更多相关文章
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- (Problem 42)Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- (Problem 74)Digit factorial chains
The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...
- (Problem 72)Counting fractions
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- (Problem 53)Combinatoric selections
There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...
- (Problem 49)Prime permutations
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...
- (Problem 47)Distinct primes factors
The first two consecutive numbers to have two distinct prime factors are: 14 = 2 7 15 = 3 5 The fi ...
- (Problem 29)Distinct powers
Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...
- (Problem 28)Number spiral diagonals
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...
随机推荐
- 高质量程序设计指南C/C++语言——C++/C程序设计入门(2)
*标准C规定,编译器只取前31个字符作为有效的标识符,而标准C++则取前255个字符作为有效的标识符. *把具有特殊含义的字符输出到终端上,尤其是当它们出现在普通字符串或格式控制字符串中时,一般来说有 ...
- 我被SQL注入撞了一下腰
网站的注入漏洞,应该说绝大多数做web开发的人都知道的事情.可是没想到从事6,7年开发工作的我,却会在这上栽了跟头,真是郁闷啊.心情很纠结,按照老婆的话,怎么感觉我像失恋了一样. 事情的起因还是在几个 ...
- UUID Gen
https://github.com/twitter/snowflake/releases/tag/snowflake-2010 http://boundary.com/blog/2012/01/12 ...
- php浮点数计算比较及取整不准确解决方法
原文:php浮点数计算比较及取整不准确解决方法 php有意思的现象,应该是很多编程语言都会有这样的现象.这个是因为计算机的本身对浮点数识别的问题..... $f = 0.58; var_dump(in ...
- JAVA GUI学习 - JTable表格组件学习_A ***
public class JTableKnow_A extends JFrame { public JTableKnow_A() { this.setBounds(300, 100, 400, 300 ...
- Java学习之DBUtils工具的学习
简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影 ...
- GTW likes math(简单数学)
GTW likes math Accepts: 472 Submissions: 2140 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- Angular JS 学习笔记(一)
1. 菜鸟教程:http://www.runoob.com/angularjs/angularjs-tutorial.html 2. Angular JS中文网:http://www.apjs.net ...
- 查看Oracle数据库某用户的连接信息
执行以下语句可查出用户TJAMIS_LXF连接信息: select schemaname, osuser, process, machine, port, terminal, program from ...
- shell 脚本中$$,$#,$?分别代表什么意思?
$0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上 ...