Skip to content

Instantly share code, notes, and snippets.

@modalsoul
Created October 15, 2013 15:04
Show Gist options
  • Select an option

  • Save modalsoul/6993025 to your computer and use it in GitHub Desktop.

Select an option

Save modalsoul/6993025 to your computer and use it in GitHub Desktop.
elixirで素数の個数判定
defmodule PrimeNumCounter do
def count(num) do
list = [2|makeList(num, [])]
res = getPrimeList(list, [], trunc(:math.sqrt(num)))
Enum.count(res)
end
def getPrimeList([h|t], result, threshold) do
if threshold >= h do
getPrimeList(sieve(h, t, []), [h|result], threshold)
else
Enum.reverse(result)++[h|t]
end
end
def sieve(num, [h|t], result) do
if rem(h, num) != 0 do sieve(num, t, [h|result])
else sieve(num, t, result)
end
end
def sieve(_, [], result) do Enum.reverse(result)
end
def makeList(num, result) do
if num==2 do result
else
makeList(num-1, [num|result])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment