-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp005.py
More file actions
33 lines (23 loc) · 694 Bytes
/
p005.py
File metadata and controls
33 lines (23 loc) · 694 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Problem 5.
Smallest multiple
https://projecteuler.net/problem=5
"""
def gcd(a, b):
"""
Find the greatest common divisor of two integers a, b.
This uses Euclid's algorithm. For integer a, b, it holds that
a * b = LCM(a, b) * GCD(a, b).
Starting from range(1, 21), we replace two head elements x, y to LCM(x, y).
"""
dividend = max(a, b)
divisor = min(a, b)
if divisor == 0:
return dividend
return gcd(dividend % divisor, divisor)
lcm_list = [*range(1, 21)]
while len(lcm_list) != 1:
lcm_head = (lcm_list[0] * lcm_list[1]) // gcd(lcm_list[0], lcm_list[1])
del lcm_list[0:2]
lcm_list.insert(0, lcm_head)
print(lcm_list[0])