Skip to content

Instantly share code, notes, and snippets.

@y-temp4
Last active November 10, 2016 01:09
Show Gist options
  • Save y-temp4/43782606960df6280fd0b7df67be79b8 to your computer and use it in GitHub Desktop.
Save y-temp4/43782606960df6280fd0b7df67be79b8 to your computer and use it in GitHub Desktop.
Project EulerのSmallest multipleをElixirで解く ref: http://qiita.com/y-temp4/items/865b17837a4fb9eaf86f
LCM = \frac{a \times b}{GCD}
defmodule Problem5 do
def gcd(a, b) do
if rem(a, b) == 0 do
b
else
gcd(b, rem(a, b))
end
end
end
IO.puts Problem5.gcd(824, 128) # 8
defmodule Problem5 do
def gcd(a, b) do
if rem(a, b) == 0 do
b
else
gcd(b, rem(a, b))
end
end
def solve(n, lcm, max) do
if n == max do
lcm
else
lcm_next = div(n * lcm, gcd(n, lcm))
solve(n+1, lcm_next, max)
end
end
end
iex(1)> 6 / 2
3.0
iex(2)> div(6, 2)
3
IO.puts Problem5.solve(2, 1, 10) # 2520
IO.puts Problem5.solve(2, 1, 20) # 232792560
defmodule Problem5 do
def gcd(x, 0), do: x
def gcd(x, y), do: gcd(y, rem(x, y))
def solve(max, lcm, max), do: lcm
def solve(n, lcm, max) do
lcm_next = div(n * lcm, gcd(n, lcm))
solve(n+1, lcm_next, max)
end
end
defmodule Problem5 do
def gcd(x, 0), do: x
def gcd(x, y), do: gcd(y, rem(x, y))
def solve(max), do: solve(2, 1, max)
def solve(max, lcm, max), do: div(max * lcm, gcd(max, lcm))
def solve(n, lcm, max) do
lcm_next = div(n * lcm, gcd(n, lcm))
solve(n+1, lcm_next, max)
end
end
IO.puts Problem5.solve(10) # 2520
IO.puts Problem5.solve(11) # 27720
IO.puts Problem5.solve(20) # 232792560
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment