-
-
Notifications
You must be signed in to change notification settings - Fork 50.3k
Expand file tree
/
Copy pathsol3.py
More file actions
51 lines (42 loc) · 1.17 KB
/
sol3.py
File metadata and controls
51 lines (42 loc) · 1.17 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Project Euler Problem 5: https://projecteuler.net/problem=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?
"""
def solution(n: int = 20) -> int:
"""
Find the smallest number for 'n'.
Iterate over each number up to n and add new factors
that is not included in previous numbers.
# >>> solution(10)
# 2520
# >>> solution(15)
# 360360
# >>> solution(22)
# 232792560
"""
# factors = {}
factors = [0] * (n + 1)
ans = 1
for i in range(2, n + 1):
if factors[i] == 0:
f = i
for j in range(2, i):
if f == 1:
break
power = factors[j]
while power > 0:
if f % j == 0:
f //= j
power -= 1
else:
break
if f != 1:
factors[f] += 1
ans *= f
return ans
if __name__ == "__main__":
print(f"{solution() = }")