0
0

More than 1 year has passed since last update.

【Project Euler】Problem 5: 最小公倍数

Last updated at Posted at 2022-01-03
  • 本記事はProjectEulerの「100番以下の問題の説明は記載可能」という規定に基づいて回答のヒントが書かれていますので、自分である程度考えてみてから読まれることをお勧めします。

問題 5.最小公倍数

原文 Problem 5: Smallest multiple

問題の要約:1から20までの数の最小公倍数を求めよ

比較的簡単な問題ですが、よく調べると2022年1月3日現在Google ColabのPythonのVersionは3.7.12で、mathモジュールにlcm()は無いようです。

! python -V
# Python 3.7.12

従ってmath.gcd()を使って作ります。

import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

print(lcm(6, 4))
# 12

このlcm()reduceに食わせると簡潔に書くことが出来ます。

from functools import reduce

print(f"Answer : {reduce(lcm, list(range(1,21)))}")
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0