Skip to content

Commit 83ad045

Browse files
committed
Add solution 3 for project euler problem 5.
1 parent 0c8cf8e commit 83ad045

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

project_euler/problem_005/sol3.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Project Euler Problem 5: https://projecteuler.net/problem=5
3+
4+
Smallest multiple
5+
6+
2520 is the smallest number that can be divided by each of the numbers
7+
from 1 to 10 without any remainder.
8+
9+
What is the smallest positive number that is _evenly divisible_ by all
10+
of the numbers from 1 to 20?
11+
"""
12+
13+
14+
def solution(n: int = 20) -> int:
15+
"""
16+
Find the smallest number for 'n'.
17+
Iterate over each number up to n and add new factors
18+
that is not included in previous numbers.
19+
20+
# >>> solution(10)
21+
# 2520
22+
# >>> solution(15)
23+
# 360360
24+
# >>> solution(22)
25+
# 232792560
26+
"""
27+
28+
# factors = {}
29+
factors = [0] * (n + 1)
30+
ans = 1
31+
for i in range(2, n + 1):
32+
if factors[i] == 0:
33+
f = i
34+
for j in range(2, i):
35+
if f == 1:
36+
break
37+
power = factors[j]
38+
while power > 0:
39+
if f % j == 0:
40+
f //= j
41+
power -= 1
42+
else:
43+
break
44+
if f != 1:
45+
factors[f] += 1
46+
ans *= f
47+
return ans
48+
49+
50+
if __name__ == "__main__":
51+
print(f"{solution() = }")

0 commit comments

Comments
 (0)