Project euler Ploblem 5 (プロジェクトオイラー5)
備忘のために残しておく。
問題
問題文 (意訳)
1~20の全ての数字で割り切れる最小の数は?
(1〜20の最小公倍数は?)
原文
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 ?
解答
答え232792560
最小公倍数を計算するだけ
Pythonのコード
Python Ploblem5
# SymPyモジュールで最大公約数、最小公倍数を計算する
import sympy
print(sympy.lcm(range(2,20+1)))
Python
# 愚直に計算する
def lcm(list_l: list) -> int:
greatest = max(list_l)
i = 1
while True:
for j in list_l:
if (greatest * i) % j != 0:
i += 1
break
else:
return greatest * i
lcm((range(1,20+1))
Juliaのコード
Julia Ploblem5
# lcm関数を使う
println(lcm(1:20))
# 愚直に計算する
function lcm_h(list_l)
greatest = maximum(list_l)
i = 1
while true
flag = false
for j in list_l
if (greatest * i) % j != 0
i += 1
flag = true
break
end
end
if flag
else
return greatest * i
end
end
end
println(lcm_h(collect(1:20)))