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
# Given an input, this program returns a list of all primes numbers less than the input. | |
defmodule NPrimes do | |
def get_primes(n) when n < 2, do: [] | |
def get_primes(n), do: Enum.filter(2..n, &is_prime?(&1)) | |
defp is_prime?(n) when n in [2, 3], do: true | |
defp is_prime?(x) do | |
start_lim = div(x, 2) | |
Enum.reduce(2..start_lim, {true, start_lim}, fn(fac, {is_prime, upper_limit}) -> |