The 3n + 1 problem

本文探讨了一种特定递归算法的周期长度计算问题,针对输入整数n,通过算法分析得出从n开始到终止条件1出现的所有数字序列的长度,并在给定范围内找出最长的周期。

Background

Problems in Computer Science are often classified as belonging to acertain class of problems (e.g., NP, Unsolvable, Recursive). In thisproblem you will be analyzing a property of an algorithm whoseclassification is not known for all possible inputs.

The Problem

Consider the following algorithm:

 
		1. 		 input n

2. print n

3. if n = 1 then STOP

4. if n is odd then tex2html_wrap_inline44

5. else tex2html_wrap_inline46

6. GOTO 2

Given the input 22, the following sequence of numbers will be printed22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured that the algorithm above will terminate (when a 1 isprinted) for any integralinput value. Despite the simplicity of the algorithm,it is unknown whether this conjecture is true. It has been verified,however, for all integersn such that 0 < n < 1,000,000 (and, in fact,for many more numbers than this.)

Given an input n, it is possible to determinethe number of numbers printed (including the 1). For a givenn this iscalled thecycle-length of n. In the example above, the cyclelength of 22 is 16.

For any two numbers i and j you are to determine the maximum cyclelength over all numbers betweeni andj.

The Input

The input will consist of a series of pairs of integers i and j, one pair ofintegers per line. All integers will be less than 1,000,000 and greaterthan 0.

You should process all pairs of integers and for eachpair determine the maximum cycle length over all integers between andincludingi andj.

You can assume that no operation overflows a 32-bit integer.

The Output

For each pair of input integers i and j you should output i, j,and the maximum cycle length for integers between and includingi andj. These three numbersshould be separated by at least one space with all three numbers on oneline and with one line of output for each line of input. The integersi andj must appear in the output in the same order in which theyappeared in the input and should befollowed by the maximum cycle length (on the same line).

Sample Input

1 10
100 200
201 210
900 1000

S

3nample Output

1 10 20
100 200 125
201 210 89
900 1000 174
 
如果是奇数则此数必然增加,如果是偶数则必然减少,特别的
当最后一位数字是2,4,8,时,该数会连续缩小两倍。如果数字
依次是奇偶奇偶,则数值会每次增加0.5倍,所以不确定最大
值会是多少,当n=159487时,最大值会达到172亿,远远超出了
int 的21亿及unsigned的42亿,所以必须考虑是否溢出,推荐用
long long 型;
<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#define LEN 1000000
typedef long long int  nint;
nint a[LEN];
nint circle(nint n)
{
	nint count=1;
	while(n>1)
	{
		if(n<LEN&&a[n])//n<LEN即如果n>1000000时a[t],t>1000000时越栈
		{
			count+=a[n]-1;//利用前面计算结果
                        break;
		}
		if(n&1)
			n=3*n+1;
		else
			n>>=1;
		count++;
	}
	return count;
}
int main()
{
	memset(a,0,sizeof(nint));
	nint j,n,m;
	nint i;
	for(i=1;i<LEN;i++)
	{
		 a[i]=circle(i);
	}
	while(scanf("%lld%lld",&n,&m)!=EOF)
	{
		nint max=0;nint t;
		printf("%lld %lld ",n,m);
		if(n>m)
		{	
			t=n;n=m;m=t;
		}
		for(j=n;j<=m;j++)
		{
			if(a[j]>max)
				max=a[j];
		}
		printf("%lld\n",max);
	}
	return 0;
}

附:n=159487 结果:
478462
239231
717694
358847
1076542
538271
1614814
807407
2422222
1211111
3633334
1816667
5450002
2725001
8175004
4087502
2043751
6131254
3065627
9196882
4598441
13795324
6897662
3448831
10346494
5173247
15519742
7759871
23279614
11639807
34919422
17459711
52379134
26189567
78568702
39284351
117853054
58926527
176779582
88389791
265169374
132584687
397754062
198877031
596631094
298315547
894946642
447473321
1342419964
671209982
335604991
1006814974
503407487
1510222462
755111231
2265333694
1132666847
3398000542
1699000271
5097000814
2548500407
7645501222
3822750611
11468251834
5734125917
17202377752
8601188876
4300594438
2150297219
6450891658
3225445829
9676337488
4838168744
2419084372
1209542186
604771093
1814313280
907156640
453578320
226789160
113394580
56697290
28348645
85045936
42522968
21261484
10630742
5315371
15946114
7973057
23919172
11959586
5979793
17939380
8969690
4484845
13454536
6727268
3363634
1681817
5045452
2522726
1261363
3784090
1892045
5676136
2838068
1419034
709517
2128552
1064276
532138
266069
798208
399104
199552
99776
49888
24944
12472
6236
3118
1559
4678
2339
7018
3509
10528
5264
2632
1316
658
329
988
494
247
742
371
1114
557
1672
836
418
209
628
314
157
472
236
118
59
178
89
268
134
67
202
101
304
152
76
38
19
58
29
88
44
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1

附:输出运算过程中国超出整形的数:

#include<stdio.h>
#include<string.h>
#define LEN 1000000
typedef long long int  nint;
nint a[LEN];
nint circle(nint n)
{
	nint k=n;
	nint count=1;
	while(n>1)
	{
		if(n<LEN&&a[n])//n<LEN即如果n>1000000时a[t],t>1000000时越栈
		{
			if(a[n]==0)
			{
				count=0;
			    break;
			}		
		}
		if(n&1)
			n=3*n+1;
		else
			n>>=1;
		if(n>2147483647)
			{
				count=0;
				break;
			}
	}
	return count;
}
int main()
{
	memset(a,1,sizeof(nint));
	nint j,n,m;
	nint i;
	for(i=1;i<LEN;i++)
		 a[i]=circle(i);
	for(i=1;i<LEN;i++)
	{
		if(a[i]==0)
			printf("%lld\n",i);
	}
	return 0;
}







内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
### 回答13n + 1问题,也称为Collatz猜想,是一个数学问题,它涉及到一个简单的算法,即对于任何正整数n,如果n是偶数,则将其除以2,否则将其乘以3再加1。这个算法会一直重复执行,直到n等于1为止。虽然这个算法看起来很简单,但至今没有人能够证明对于所有正整数n都能够最终收敛到1。这个问题一直是数学界的一个谜题,吸引了许多数学家的关注和研究。 ### 回答2: “3n+1问题”也称为“Collatz猜想”,是解决数学领域中的一项经典问题。这个问题是对于每个正整数n,按照以下规则不断进行计算: (1) 如果n为奇数,计算3n +1; (2) 如果n为偶数,计算n/2; (3) 重复以上过程,直到n等于1为止。 猜想:无论初始值n是多少,经过有限次计算后一定会得到1。 在过去几十年中,无数数学家尝试用各种方法解决这个问题,但目前仍未找到证据切实有效的方法来证明猜想的正确性。 为此,勒内·斯塔迪在2005年为完全确定“3n+1问题”而创建了数字时限计划 ,该计划采用自由计算和验证方法,依赖众多志愿者的合作来解决这个难题。 事实上,这个问题并不仅仅只涉及到数学领域,它也有着重要的意义和应用价值。例如,可以通过优化算法来提高计算机的效率,甚至有人认为它与宇宙的本质有关。埃尔顿·M. 科贝尔因为此问题,在他的研究中发展出了一个术语“随机行走的周期时间”,并在研究自然环境的变化中提出了该术语的应用。 虽然迄今为止,人们仍未找到一个能够证明猜想正确与否的方法,但大量众志成城的项目,以及各种讨论和探寻的努力,让人们在探索数学的过程中,不断拓展了对于科学、技术、人类文化的认识和思考。 ### 回答33n+1问题(The 3n+1 problem),也叫做Collatz猜想,是指对于所有正整数n,按照如下规则进行递归操作。如果n是偶数,则把它除以2得到n/2;如果n是奇数,则把它乘以3再加1得到3n+1。按照这样的规则反复操作,最终可以得到一个数列1,4,2,1,4,2,1……其中1是最终结果,也是所有数字都会到达的终点。 尽管3n+1问题规则很简单,但它一直是一个困扰数学家的难题。目前还没有人能够证明这个所有数字最终都可以到达1的规律,也没有人能够找到一个能够对所有数字进行快速计算的算法。因此,这个问题吸引了众多数学家和计算机科学家的关注,成为了一个经典的数学难题。 3n+1问题的研究不仅仅是理论的,也具有实际应用价值。例如,在计算机设计中,3n+1问题可以用于测试计算机处理器的运算能力和稳定性。实际上,世界上许多大型计算机系统都会对3n+1问题进行测试。 总之,3n+1问题是一个经典而复杂的数学难题,尽管它至今没有被完全证明,但是我们仍然可以通过研究和计算来探究这个问题,不断拓展我们对数论和计算机科学的认识和理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值