zoukankan      html  css  js  c++  java
  • 欧拉计划第12题题解

    Highly divisible triangular number

    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?

    高度可约的三角形数

    三角形数数列是通过逐个加上自然数来生成的。例如,第7个三角形数是 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28。三角形数数列的前十项分别是:

    1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …
    让我们列举出前七个三角形数的所有约数:

    • 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

    我们可以看出,28是第一个拥有超过5个约数的三角形数。

    第一个拥有超过500个约数的三角形数是多少?

    解题思路

    (1) 开始枚举每一个数,判断它有多少个约数。

    判断约数存在 (O( sqrt{n} )) 时间复杂度的算法:

    我们可以从 (1)(lfloor sqrt{n} floor) 去枚举每一个数 (i),如果 (i) 能整除 (n),则 (frac{n}{i}) 也能够整除 (n)

    实现代码如下(其中 cal(n) 用于返回n的约数个数):

    #include <bits/stdc++.h>
    using namespace std;
    int cal(long long n) {
        int cnt = 0;
        for (long long i = 1; i*i <= n; i ++) {
            if (n % i == 0) {
                if (i*i == n) cnt ++;
                else cnt += 2;
            }
        }
        return cnt;
    }
    int main() {
        int n = 0;
        for (long long i = 1; ; i ++) {
            n += i;
            if (cal(n) > 500) {
                cout << n << endl;
                break;
            }
        }
        return 0;
    }
    

    得到答案为 (76576500)

  • 相关阅读:
    HttpURLConnection中使用代理(Proxy)及其验证(Authentication)
    Java获取随机数的几种方法
    史上最全的java随机数/字符串生成算法(转)
    httpClient中的三种超时设置小结
    几个性能测试工具
    硬件知识
    北风风hadoop课程体系
    IT大数据服务管理高级课程(IT服务,大数据,云计算,智能城市)
    .net framework client profile
    Resharper中注释代码的快捷键
  • 原文地址:https://www.cnblogs.com/quanjun/p/12323196.html
Copyright © 2011-2022 走看看