• Project Euler Problem 12: Highly divisible triangular number


    Highly divisible triangular number

    Problem 12

    The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

    1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

    Let us list the factors of the first seven triangle numbers:

     1: 1

     3: 1,3

     6: 1,2,3,6

    10: 1,2,5,10

    15: 1,3,5,15

    21: 1,3,7,21

    28: 1,2,4,7,14,28

    We can see that 28 is the first triangle number to have over five divisors.

    What is the value of the first triangle number to have over five hundred divisors?


    C++:

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    const int FIVE_HUNDRED = 500;
    
    int count(int sum)
    {
        if(sum == 1)
            return 1;
    
        int ans = 0, end;
        end = sqrt(sum);
        for(int i=1; i<end; i++)
            if(sum % i == 0)
                ans++;
        ans <<= 1;      // ans = ans * 2;
        if(end * end == sum)
            ans++;
    
        return ans;
    }
    
    int main()
    {
        for(int i=1, sum=1; ; ) {
            if(count(sum) > FIVE_HUNDRED) {
                cout << sum << endl;
                break;
            }
            sum += ++i;
        }
    
        return 0;
    }


    C++:

    #include <iostream>
    
    using namespace std;
    
    //#define DEBUG
    
    const int FIVE_HUNDRED = 500;
    
    int count(int sum)
    {
        if(sum == 1)
            return 1;
    
        int ans = 0;
        for(int i=1, end=sum/2; i<end; i++)
            if(sum % i == 0) {
                ans += 2;
                if(i * i == sum)
                    ans--;
                end = sum / i;
            }
    
        return ans;
    }
    
    int main()
    {
        for(int i=1, sum=1; ; ) {
    #ifdef DEBUG
            cout << sum << " " << count(sum) << endl;
    #endif
            if(count(sum) > FIVE_HUNDRED) {
                cout << sum << endl;
    
                break;
            }
    
            sum += ++i;
        }
    
        return 0;
    }




  • 相关阅读:
    激活函数(ReLU, Swish, Maxout)
    损失函数
    md5sum命令行使用注意事项
    Jetson ARM SeetaFace编译
    Linux下的wine生活(QQ/微信/Office)
    人脸识别引擎SeetaFace编译 ubuntu
    Python为8bit深度图像应用color map
    MySQL、MongoDB、Redis数据库Docker镜像制作
    bash的管道符与重定向
    Docker 及 nvidia-docker 使用
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564026.html
Copyright © 2020-2023  润新知