Project Euler

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Project Euler相关的知识,希望对你有一定的参考价值。

?????????stat   static   ?????????   ace   include   init   move   std   gcd   

??????

P1

??????
?????????1000?????????3???5??????????????????

??????

#include <bits/stdc++.h>
using namespace std;

int get(int x) {
    return (x+1)*x/2;
}

int solve(int n) {
    return get(n/3)*3+get(n/5)*5-get(n/15)*15;
}

int main() {
    cout << solve(999) << endl;
    return 0;
}

P2

??????
???????????????4e6??????????????????????????????????????????

??????

#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> fib({1, 2});
    long long res = 2;
    while (1) {
        int x = fib.end()[-2] + fib.end()[-1];
        if (x > 4000000) break;
        if (~x & 1) res += x;
        fib.push_back(move(x));
    }
    cout << res << endl;
    return 0;
}

P3

??????
???600851475143?????????????????????
??????

#include <bits/stdc++.h>
using namespace std;

const long long Target = 600851475143;
const int N = sqrt(Target);

vector<int> init_prime() {
    static bool vis[N];
    vector<int> p;
    for (int i = 2; i < N; ++i) {
        if (!vis[i]) p.push_back(i);
        for (int j = 0; j < p.size() && p[j]*i < N; ++j) {
            vis[p[j]*i] = 1;
            if (i % p[j] == 0) break;
        }
    }
    return move(p);
}

int main() {
    auto p = init_prime();
    long long n = Target;
    long long res = 1;
    for (auto &x : p) {
        if (n % x == 0) res = x;
        while (n % x == 0) n /= x;
    }
    if (n > res) res = n;
    cout << res << endl;
    return 0;
}

P4

??????
???????????????????????????????????????????????????????????????
??????

#include <bits/stdc++.h>
using namespace std;

bool check(int x) {
    vector<int> d;
    do d.push_back(x % 10), x /= 10; while (x);
    for (int i = 0; i + i < d.size(); ++i)
        if (d[i] != d.end()[-i - 1]) return 0;
    return 1;
}

int main() {
    int res = 0;
    for (int x = 100; x < 1000; ++x)
        for (int y = 100; y < 1000; ++y)
            if (check(x * y)) res = max(res, x * y);
    cout << res << endl;
    return 0;
}

P5

??????
???1~20?????????????????????
??????

#include <bits/stdc++.h>
using namespace std;

int main() {
    long long lcm=1;
    for (int i=1;i<=20;++i)
        lcm=lcm*i/__gcd(1ll*i,lcm);
    cout<<lcm<<endl;
    return 0;
}

P6

??????
??? $(1+2+cdots +100)^2-(1^2+2^2+cdots +100^2)$
??????

#include <bits/stdc++.h>
using namespace std;

int main() {
    int sum=5050;
    int res=0;
    for (int i=1;i<=100;++i)
        res+=i*(sum-i);
    cout<<res<<endl;
    return 0;
}

以上是关于Project Euler的主要内容,如果未能解决你的问题,请参考以下文章

Python Project Euler数字第五种力量

Project Euler 75: Singular integer right triangles

Project-Euler (Make/Source) 的有用文件夹结构? [关闭]

Project Euler 453 Lattice Quadrilaterals 困难的计数问题

Project Euler 109 :Darts 飞镖

Project Euler 5