1077 -- The 3n + 1 problem

The 3n + 1 problem

Time Limit:1000MS  Memory Limit:65536K
Total Submit:75 Accepted:25

Description

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

Consider the following algorithm:


1. input n

2. print n

3. if n = 1 then STOP

4. if n is odd then n <- 3n + 1

5. else n <- n / 2

6. GOTO 2


Given the input 22, the following sequence of numbers will be printed 22 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 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)

Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.

For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.

Input

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

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.

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

Output

For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).

Sample Input

1 10
100 200
201 210
900 1000

Sample Output

1 10 20
100 200 125
201 210 89
900 1000 174

Source

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace AK1077 {
        /// <summary>
        /// 找一个范围内转换次数最多的次数输出
        /// </summary>
        class Program {
            static int tsy(int a) {
                int count = 1;
                while (a != 1) {
                    if (a % 2 == 1) { a = 3 * a + 1; count++; } else { a /= 2; count++; }
                }
                return count;
            }
            static void Main(string[] args) {
                string sb;
                while ((sb = Console.ReadLine()) != null) {
                    string[] s = sb.Split();
                    int n = int.Parse(s[0]), m = int.Parse(s[1]);
                    int n1 = n, m1 = m;
                    if (n > m) {
                        int temp = m;
                        m = n;
                        n = temp;
                    }
                    int max = -1;
                    for (int i = n; i <= m; i++) {
                        int count = tsy(i);
                        if (count > max) max = count;
                    }
                    Console.WriteLine("{0} {1} {2}", n1, m1, max);
                }
            }
        }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 3n + 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. 科贝尔因为此问题,在他的研究中发展出了一个术语“随机行走的周期时间”,并在研究自然环境的变化中提出了该术语的应用。 虽然迄今为止,人们仍未找到一个能够证明猜想正确与否的方法,但大量众志成城的项目,以及各种讨论和探寻的努力,让人们在探索数学的过程中,不断拓展了对于科学、技术、人类文化的认识和思考。 ### 回答3: 3n+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、付费专栏及课程。

余额充值