(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:
40
20
10
5
16
8
4
2
1It 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开始,我们得到以下序列:
40
20
10
5
16
8
4
2
1可以看出这个以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 ...
随机推荐
- python安装集成包
anaconda, 包含各种科学运算包以及astropy.装完它一劳永逸. https://www.continuum.io/downloads
- HTML+CSS笔记 CSS笔记集合
HTML+CSS笔记 表格,超链接,图片,表单 涉及内容:表格,超链接,图片,表单 HTML+CSS笔记 CSS入门 涉及内容:简介,优势,语法说明,代码注释,CSS样式位置,不同样式优先级,选择器, ...
- java中文乱码问题
•ASCII ØASCII使用7个比特(bit)进行字符编码,最多可以表示的字符数量为27(128)个字符,这些字符包括了大小写英文字母.阿拉伯数字.标点符号.控制字符和其他符号.每个字符用一个字节表 ...
- 几款Http小服务器
六款小巧的HTTP Server[C语言] http://blog.linuxphp.org/?action=show&id=60 转载:
- 基于Visual C++2013拆解世界五百强面试题--题10-找出N个数种最大的K个数
有一亿个整数,请找出最大的 1000 个,要求时间越短越好, 空间占用越好越好. 如果不考虑时间效率,很容易想到解决方法,我们只需存储前一千个数, 然后依次读入后面的数和这一千个数组比较,替换其中比较 ...
- Android测试TestSuite的执行方法
public class StartTest extends InstrumentationTestRunner { public TestSuite getAllTests() { ...
- Android学习笔记(十七)——使用意图调用内置应用程序
使用意图调用内置应用程序 1.创建一个新的Android项目并命名为Intents,在main.xml文件里加入两个Button: <Button android:id="@+id/b ...
- Eclipse新建Android工程,在模拟器运行的时候提示Unfortunately,XXX has stopped.
刚新建好的android工程在模拟器运行的时候出错,提示Unfortunately,XXX has stopped 查看Eclipse下面的错误信息,双击第一条 把ActionBarActivity前 ...
- [译]Stairway to Integration Services Level 7 - SSIS 工作流管理中级
介绍 前一个章节我们对SSIS的优先约束做了初步了解,并且实验了MaxConcurrentExecutables 包属性的设置. 本文将测试“On Success”, “On Completion”, ...
- vs调试MEX文件
http://www.cnblogs.com/lukylu/p/4042306.html matlab里面无法单步调试mex函数,故需转到VS上面调试,这里采用VS2010. 参考网上很多人写的方法但 ...