Problem 5:Smallest multiple
标签:最大公约数、最小公倍数
原文: 2520 2520 2520 is the smallest number that can be divided by each of the numbers from 1 1 1 to 10 10 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 1 1 to 20 20 20?
翻译: 2520 2520 2520 是最小的能够被 1 1 1 到 10 10 10 整除的正数。
最小的能够被 1 1 1 到 20 20 20 整除的正数是多少?
题解:很显然是求最小公倍数。
最大公约数( g r e a t e s t c o m m o n d i v i s o r greatest\ common \ divisor greatest common divisor): g c d ( a , b ) = g c d ( b , a % b ) gcd(a,b)=gcd(b,a\%b) gcd(a,b)=gcd(b,a%b)
最小公倍数( l e a s t c o m m o n m u l t i p l e least \ common \ multiple least common multiple): l c m ( a , b ) = a ∗ b / g c d ( a , b ) lcm(a,b)=a*b/gcd(a,b) lcm(a,b)=a∗b/gcd(a,b)
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
ll k = 1;
for (ll i = 1; i <= 20; i++) {
k = k * i / gcd(k, i);
}
cout << k << endl;
return 0;
}
“Project Euler exists to encourage, challenge, and develop the skills and enjoyment of anyone with an interest in the fascinating world of mathematics.”
“欧拉计划的存在,是为了每个对数学感兴趣的人,鼓励他们,挑战他们,并最终培养他们的能力与乐趣。”