zoukankan      html  css  js  c++  java
  • Project Euler Problem 14 Longest Collatz sequence

    Longest Collatz sequence

    Problem 14

    The following iterative sequence is defined for the set of positive integers:

    nn/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?

    NOTE: Once the chain starts the terms are allowed to go above one million.


    C++:

    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    const int MAXN = 1000000;
    const int ONE_HUNDRED_MILLION = 100000000;
    
    int cs[ONE_HUNDRED_MILLION+1];
    
    // Collatz sequence count
    int cscount(long long x)
    {
        if(x <= ONE_HUNDRED_MILLION && cs[x])
            return cs[x];
    
        int count;
        if(x % 2 == 0)
            count = 1 + cscount(x / 2);
        else
            count =  1 + cscount(x * 3 + 1);
    
        if(x <= ONE_HUNDRED_MILLION)
            cs[x] = count;
    
        return count;
    }
    
    int main()
    {
        memset(cs, 0, sizeof(cs));
        cs[1] = 1;
    
        int n, ans;
        while(cin >> n && n <= MAXN) {
            ans = 1;
            for(int i=1; i<=n; i++) {
                cs[i] = cscount(i);
                if(cs[i] > cs[ans])
                    ans = i;
            }
            cout << ans << endl;
        }
    
        return 0;
    }

    Input data:

    999999



    C++(Too slow):

    #include <iostream>
    
    using namespace std;
    
    //#define DEBUG
    
    const int MAXN = 1000000;
    
    // Collatz sequence count
    int cscount(int start)
    {
    #ifdef DEBUG
            cout << start << ": ";
    #endif
    
        int count = 0;
        for(;;) {
    #ifdef DEBUG
            cout << start << " ";
    #endif
    
            count++;
            if(start == 1)
                break;
            if(start & 1)
                start = 3 * start + 1;
            else
                start >>= 1;
        }
    
    #ifdef DEBUG
        cout << endl;
    #endif
    
        return count;
    }
    
    int main()
    {
        int n, ans, maxcount=0, temp;
        while(cin >> n && n <= MAXN) {
            for(int i=1; i<=n; i++) {
                temp = cscount(i);
                if(temp > maxcount) {
                    maxcount = temp;
                    ans = i;
                }
            }
            cout << ans << endl;
        }
    
        return 0;
    }




  • 相关阅读:
    Netty ByteBuf(图解之 2)| 秒懂
    【转】 RGB各种格式
    缺少动态库报错
    bug:进程可调用函数而子线程调用报错
    【转】 pthread设置线程的调度策略和优先级
    【转】 C++析构函数的作用和用法
    【转】 g++编译时对'xxxx'未定义的引用问题(undefined reference to)
    【转】 C语言文件操作详解
    【转】 H.264编码原理以及I帧B帧P帧
    【转】 strrchr()函数---C语言
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564020.html
Copyright © 2011-2022 走看看