Last active
November 10, 2016 01:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LCM = \frac{a \times b}{GCD} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Problem5 do | |
def gcd(a, b) do | |
if rem(a, b) == 0 do | |
b | |
else | |
gcd(b, rem(a, b)) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IO.puts Problem5.gcd(824, 128) # 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(1)> 6 / 2 | |
3.0 | |
iex(2)> div(6, 2) | |
3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IO.puts Problem5.solve(2, 1, 10) # 2520 | |
IO.puts Problem5.solve(2, 1, 20) # 232792560 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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