`

Problem14

 
阅读更多

题目: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?

(克拉茨问题,求一百万内的一个数,使其经过克拉茨问题的运算后得到的序列最长)

 

 

public class Problem14 {
	private int[] counts;
	private int num;
	
	public Problem14(int num){
		this.num = num;
		counts = new int[num];
	}
	
	public static void main(String[] args) {
		int range = 1000000;
		Problem14 p = new Problem14(range);
		int cmp1 = 0;
		int cmp2 = 0;
		int r = 0;
		int tmp = 0;
		
		long st1 = System.nanoTime();
		for(int i = 1; i<=range; i++){
			tmp = p.comlen(i);
			if(tmp>cmp1){
				cmp1 = tmp;
				r = i;
			}

		}
		
		long et1= System.nanoTime();
		System.out.println(r);
		
		/*
		long st2 = System.nanoTime();
		for(int i = 1; i<=range; i++){
			tmp = p.len(i);
			if(tmp>cmp2){
				cmp2 = tmp;
				r = i;
			}		
		}
		
		long et2= System.nanoTime();
		System.out.println(r);
		*/
		//System.out.println(p.getCounts(1000000));
		System.out.println(et1-st1);
		//System.out.println(et2-st2);
		
		
	}

	public int len(int n){
		long copy = n;
		int len = 1;
		while(copy != 1){
			if(copy%2==0){
				
				copy /= 2;
				if(copy<=num){
					int flag = (int)copy;
					if(counts[flag-1] != 0){
						len += counts[flag-1];
						break;
					}
				}
				len++;
			}else{
				
				copy = 3*copy+1;
				if(copy<=num){
					int flag = (int)copy;
					if(counts[flag-1] != 0){
						len += counts[flag-1];
						break;
					}
				}
				len++;
			}	
		}
		counts[n-1] = len;
		return len;	
	}
	/*
	public int comlen(int n){
		long copy = n;
		int len  = 1;
		while(copy != 1){
			if(copy%2==0)
				copy /= 2;
			else
				copy = 3*copy+1;
			len++;
		}
		return len;
	}
	*/

}
 

我的想法是这样的:

 

按照常规做法,只需计算一百万内各个自然数的Collatz数列,然后比较即可。但这样其实重复了一些过程,比如题中例子:

 

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

 

自然数从13到20的运算是是之前没出现过的,但到了自然数10,不难发现在计算13之前是计算过10的Collatz数列的长度

 

的,所以这个数列计算到10其实可以不必计算了,只需把预先存储起来10的Collatz数列的长度加上13到20这过程中产生

 

自然数个数就可以了。

 


分享到:
评论

相关推荐

    MySQL数据库考试试题.docx

    14. 主键的建立方法:三种(Problem 14) 主键可以使用 PRIMARY KEY 约束、UNIQUE 约束或索引来建立。 15. 视图上的限制操作:不能定义新的基本表(Problem 15) 视图是一个基于表的虚拟表,不能在视图上定义新的...

    euler project.r.zip_R Euler project_project

    14. **Problem 14: 长链回文子串** - 知识点:字符串处理,回文判断,循环结构。 - 解决方法:计算每个数的回文子串长度,找到最长的那个。 通过解决这些问题,你可以掌握R语言的基本语法、数据类型、控制流、...

    Computer-Based.Problem.Solving.Process

    Chapter 14. Web-Based Problem Solving Process Chapter 15. Software Tool Development Illustration Chapter 16. Software Tools for Correct Program Development Part 5 Computer Operation by Problem ...

    Java经典问题算法大全

    13.Algorithm Gossip: 背包问题(Knapsack Problem 14.Algorithm Gossip: 蒙地卡罗法求 PI 15.Algorithm Gossip: Eratosthenes 筛选求质数 16.Algorithm Gossip: 超长整数运算(大数运算). 17.Algorithm Gossip: 长...

    计算机网络第六版答案

    14. If the two ISPs do not peer with each other, then when they send traffic to each other they have to send the traffic through a provider ISP (intermediary), to which they have to pay for carrying ...

    C++ Programming From Problem Analysis to Program Design 8th

    《C++ Programming From Problem Analysis to Program Design 8th》是一本深入浅出的C++编程教程,由资深计算机教育家Zelle撰写。该书旨在帮助读者从问题分析到程序设计的整个过程,全面掌握C++语言。最新版更新至...

    江苏省永丰初级中学九年级英语上册Unit3Teenageproblems复习讲义无答案新版牛津版

    13. 成为问题的原因 - Become a problem 14. 担忧时间 - Worry about time 15. 陷入困境 - Get into trouble 16. 允许某人做某事 - Allow sb. to do sth. 17. 对某人要求严格 - Be strict with sb. 18. 在外呆的很迟...

    Problem - 1001.pdf

    在实现算法时,需要注意效率,因为题目允许的时间限制是14秒(对于Java等非C++语言是7秒),内存限制是512MB。因此,需要优化算法,避免不必要的计算和空间浪费。可以考虑使用高效的数据结构和优化的合并策略,如...

    Artificial Intelligence and Problem Solving

    14 Map Coloring & Chromatic Number......Page 271 iLLusTrATinG The TheOrem......Page 273 eArLy ATTemPTs AT PrOOf......Page 275 summAry Of evenTs ThAT LeD TO The DefiniTiOn AnD sOLuTiOn TO The fOur ...

    Fast Neighborhood Search for the Nesting Problem

    Benny Kjr Nielsen and Allan Odgaard fbenny, dug@diku.dk February 14, 2003 The main subject of this thesis is the so-called nesting problem, which (in short) is the problem of packing arbitrary two-...

    机器学习基石10 - 1 - Logistic Regression Problem (14-33).mp4

    机器学习基石10 - 1 - Logistic Regression Problem (14-33).mp4

    Prime Ring Problem 深度探索

    int M, g, a[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, ag[20], c[20]; ``` - 初始化数组`a`,用于存储可能填入环中的数字。 - `ag`数组用于标记数字是否已被使用。 - `c`...

    Problem Solving with C++

    #### 第14章:递归与栈(p.801) 递归是一种强大的编程技术,可以简化许多算法的设计。本节解释了递归的基本原理以及如何使用递归函数。 - **递归**:函数调用自身的过程。 - **递归基**:递归终止条件,确保递归...

    C++.Programming.From.Problem.Analysis.to.Program.Design.7th.Edition

    Chapter 14. Exception Handling. Chapter 15. Recursion. Chapter 16. Searching and Sorting. Chapter 17. Linked Lists. Chapter 18. Stacks and Queues. Appendix A. Reserved Words. Appendix B. Operator ...

    Zero Duality Gap in Optimal Power Flow Problem

    by the standard IEEE benchmark systems with 14, 30, 57, 118, and 300 buses as well as several randomly generated systems. Since this condition is hard to study, a sufficient zero-duality-gap condition...

    Problem Solving in Data Structures & Algorithms Using Java

    CHAPTER 14: STRING ALGORITHMS CHAPTER 15: ALGORITHM DESIGN TECHNIQUES CHAPTER 16: BRUTE FORCE ALGORITHM CHAPTER 17: GREEDY ALGORITHM CHAPTER 18: DIVIDE-AND-CONQUER, DECREASE-AND-CONQUER CHAPTER 19: ...

    Problem Solving with C++ (7th edition)

    #### Chapter 14: Recursion This chapter introduces recursion, a powerful programming technique: - **Recursive Functions**: Explanation of recursive functions, including base cases and recursive ...

    0-1-knapsack-problem-master (14).zip

    标题 "0-1-knapsack-problem-master (14).zip" 提示我们这是一个关于 0-1 背包问题的项目,而描述 "近似求π" 可能意味着这个项目中包含了利用 0-1 背包问题的算法来估算圆周率 π 的一个实现。0-1 背包问题是一个...

Global site tag (gtag.js) - Google Analytics