## 为什么Elixir在Ruby中最慢并且在解决规划上面走得很慢？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (47)

1. go 1.4.2：0.58s
2. Ruby 2.2 MRI：6.7s
3. Elixir 1.0.5（我的第一个算法）：57s
4. Elixir 1.0.5（我的第一个MIX_ENV = prod前缀的算法）：7.4s
5. Elixir 1.0.5（Roman's Go等效算法）：0.7s
6. Elixir 1.0.5（Roman的Ruby等价算法）：1.8s

``````func problem005() int {
i := 20
outer:
for {
for j := 20; j > 0; j-- {
if i%j != 0 {
i = i + 20
continue outer
}
}
return i
}
panic("Should have found a solution by now")
}
``````

``````def self.problem005
divisors = (1..20).to_a.reverse

number = 20 # we iterate over multiples of 20

until divisors.all? { |divisor| number % divisor == 0 } do
number += 20
end

return number
end
``````

``````def problem005 do
divisible_all? = fn num ->
Enum.all?((20..2), &(rem(num, &1) == 0))
end

Stream.iterate(20, &(&1 + 20))
|> Stream.filter(divisible_all?)
|> Enum.fetch! 0
end
``````

### 2 个回答

``````defmodule Euler do
@max_divider 20
def problem005 do
problem005(20, @max_divider)
end

defp problem005(number, divider) when divider > 1 do
if rem(number, divider) != 0 do
problem005(number+20, @max_divider)
else
problem005(number, divider-1)
end
end
defp problem005(number, _), do: number
end
``````

``````def problem_005
divisor = max_divisor = 20
number = 20 # we iterate over multiples of 20

while divisor > 1 do
if number % divisor == 0
divisor -= 1
else
number += 20
divisor = max_divisor
end
end

number
end
``````

``````defmodule Euler do
def problem005 do
problem005(20)
end

@divisors (20..2) |> Enum.to_list
defp problem005(number) do
if Enum.all?(@divisors, &(rem(number, &1) == 0)) do
number
else
problem005(number+20)
end
end
end
``````