Skip to content

Add solution 3 for project euler problem 5. #12763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions project_euler/problem_005/sol3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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() = }")