Solution to problem 5 in Julia
This commit is contained in:
parent
30767b888e
commit
5240feb3c9
41
src/Julia/Problem005.jl
Normal file
41
src/Julia/Problem005.jl
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#=
|
||||||
|
Created on 20 Jun 2021
|
||||||
|
|
||||||
|
@author: David Doblas Jiménez
|
||||||
|
@email: daviddoji@pm.me
|
||||||
|
|
||||||
|
Solution for Problem 5 of Project Euler
|
||||||
|
https://projecteuler.net/problem=5
|
||||||
|
=#
|
||||||
|
|
||||||
|
#=
|
||||||
|
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))
|
||||||
|
=#
|
||||||
|
|
||||||
|
|
||||||
|
function Problem5()
|
||||||
|
#=
|
||||||
|
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 1:21
|
||||||
|
ans *= i ÷ gcd(i, ans)
|
||||||
|
end
|
||||||
|
|
||||||
|
return ans
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
println("Time to evaluate Problem 5:")
|
||||||
|
@time Problem5()
|
||||||
|
println("")
|
||||||
|
println("Result for Problem 5: ", Problem5())
|
Loading…
x
Reference in New Issue
Block a user