Solution for problem 5
This commit is contained in:
parent
98006398d9
commit
7edb47e425
41
src/Python/Problem005.py
Normal file
41
src/Python/Problem005.py
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Created on 23 Apr 2017
|
||||||
|
|
||||||
|
@author: David Doblas Jiménez
|
||||||
|
@email: daviddoji@pm.me
|
||||||
|
|
||||||
|
Solution for problem 5 of Project Euler
|
||||||
|
https://projecteuler.net/problem=5
|
||||||
|
"""
|
||||||
|
|
||||||
|
import math
|
||||||
|
from utils import timeit
|
||||||
|
|
||||||
|
# The LCM of two natural numbers x and y is given by:
|
||||||
|
# def lcm(x, y):
|
||||||
|
# return x * y // math.gcd(x, y)
|
||||||
|
|
||||||
|
# It is possible to compute the LCM of more than two numbers by iteratively
|
||||||
|
# computing the LCM of two numbers, i.e. LCM(a, b, c) = LCM(a, LCM(b, c))
|
||||||
|
|
||||||
|
|
||||||
|
@timeit("Problem 5")
|
||||||
|
def compute():
|
||||||
|
"""
|
||||||
|
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?
|
||||||
|
"""
|
||||||
|
ans = 1
|
||||||
|
for i in range(1, 21):
|
||||||
|
ans *= i // math.gcd(i, ans)
|
||||||
|
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
print(f"Result for problem 5: {compute()}")
|
Loading…
x
Reference in New Issue
Block a user