Project Euler 5

Project Euler 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$?

解决方案

根据定义,所求的值为$1\sim20$中间的所有数的最小公倍数$\text{lcm}$。

求多个数的$\text{lcm}$和求多个数的最大公因数$\gcd$做法一样,都是两两按顺序求。

使用了gmpy2库中的lcm方法,以后将封装到tools工具包中。

代码

1
2
3
4
5
6
7
8
from gmpy2 import lcm

N = 20
ans = 1
for i in range(1, 21):
ans = lcm(ans, i)
print(ans)

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
Ujimatsu Chiya 微信 微信
Ujimatsu Chiya 支付宝 支付宝