欧拉计划第5题:Smallest multiple(最大公约数 最小公倍数)

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)=ab/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.”

“欧拉计划的存在,是为了每个对数学感兴趣的人,鼓励他们,挑战他们,并最终培养他们的能力与乐趣。”

  • 18
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值