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)

  • 相关阅读:
    bootstrap模态框
    css 禁止选中文本
    Python Flask Tornado
    JS canvas标签动态绘制图型
    JS 跳转页面
    JS 计算器
    JS
    柱状图中最大的矩形
    在不使用第三个变量的情况下交换两个数的值
    springboot配置静态资源访问的2种方式
  • 原文地址:https://www.cnblogs.com/quanjun/p/12323196.html
Copyright © 2011-2022 走看看