Last active
August 29, 2015 14:27
-
-
Save chooblarin/98bf62a8a9e7897daf77 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Prac2 do | |
def prime?(2) do true end | |
def prime?(n) when n < 2 do false end | |
def prime?(n) when rem(n, 2) == 0 do false end | |
def prime?(n) do check(3, n) end | |
defp check(a, b) do | |
if a * a <= b do | |
if rem(b, a) == 0 do | |
false | |
else | |
check(a + 2, b) | |
end | |
else | |
true | |
end | |
end | |
end | |
# 1..100 |> Enum.filter(&(Prac2.prime?/1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment