欧拉计划第5题题解

Smallest multiple

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

最小公倍数

2520是最小的能够被1到10整除的数。

最小的能够被1到20整除的正数是多少?

解题思路

其实这道题就是求 (1)(20)(20) 个数的最小公倍数是多少。

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
long long lcm(long long a, long long b) {
    return a / __gcd(a, b) * b;
}
int main() {
    long long ans = 1;
    for (int i = 2; i <= 20; i ++) ans = lcm(ans, i);
    cout << ans << endl;
    return 0;
}

得到答案为 (232792560)

原文地址:https://www.cnblogs.com/quanjun/p/12322798.html