Problem 5: Smallest multiple

Problem 5

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 至 20 中每个数整除的数。实际上就是求它们的最小公倍数。在 Haskell 交互环境直接计算:

Prelude> foldl1 lcm [1..20]

上述程序中:

  • lcm 函数返回其两个自变量的最小公倍数。

  • foldl1 函数从左至右遍历列表 [1..20],对列表中的每个元素依次应用 lcm 函数。

改进

上述程序中的 [1..20] 可以修改为 [2..20],因为 1 可以整除任何整数。实际上,还可以进一步修改为 [11..20],这是因为以下等式:

        lcm

证明思路如下:对于 1 至 n_2 之间的每个数 a,在 n_2_1 至 n 之间刚好可以找到一个数 b,使得 2ka = b 。

用纸和笔计算

只需要计算从 20 下降到 11 的诸数的最小公倍数:

  • 20

  • 19

  • 18 = 2 × 9

  • 17

  • 16 = 4 × 4

  • 15 = 3 × 5

  • 14 = 2 × 7

  • 13

  • 12 = 3 × 4

  • 11

因此,所求的数就是 20 × 19 × 9 × 17 × 4 × 7 × 13 × 11,这很容易笔算出来。

相关资源

可参阅:005_overview.pdf

posted @ 2020-11-26 04:05  璃奈ちゃんボード  阅读(95)  评论(0编辑  收藏  举报