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

本文讨论了如何找到1到20之间能被所有数字整除的最小正数,即最小公倍数,利用了欧拉问题5的解题思路,包括最大公约数的计算和最小公倍数的公式。并通过C++代码实现算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 5:Smallest multiple

标签:最大公约数、最小公倍数

原文252025202520 is the smallest number that can be divided by each of the numbers from 111 to 101010 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 111 to 202020

翻译252025202520 是最小的能够被 111101010 整除的正数。

最小的能够被 111202020 整除的正数是多少?

题解:很显然是求最小公倍数。

最大公约数(greatest common divisorgreatest\ common \ divisorgreatest common divisor):gcd(a,b)=gcd(b,a%b)gcd(a,b)=gcd(b,a\%b)gcd(a,b)=gcd(b,a%b)

最小公倍数(least common multipleleast \ common \ multipleleast common multiple):lcm(a,b)=a∗b/gcd(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.”

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值