Skip to content

Instantly share code, notes, and snippets.

@modalsoul
Last active December 25, 2015 11:39
Show Gist options
  • Select an option

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

Select an option

Save modalsoul/6970624 to your computer and use it in GitHub Desktop.
elixirで素数判定
defmodule PrimeNum do
def isPrime(num) do
if num==1 || rem(num, 2)==0 do
false
else
x = makeList(trunc(:math.sqrt(num)), [])
sieve(x, num)
end
end
def sieve([h|t], num) do
if rem(num,h)==0 do false
else sieve(t, num)
end
end
def sieve([], _) do true
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