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)

  • 相关阅读:
    vue项目实现路由按需加载
    常用的meta标签
    聊聊https
    Centos 7 忘记root密码修改方法
    find常用命令
    linux上的mysql忘记密码
    kaill 安装zenmap软件
    selenium.common.exceptions.WebDriverException: Message: 'chromedriver'解决
    centos7 开机执行脚本或者命令
    Linux下载常用命令
  • 原文地址:https://www.cnblogs.com/quanjun/p/12323196.html
Copyright © 2011-2022 走看看