Last active
February 1, 2021 09:48
-
-
Save thebugcatcher/2487b9ed7f70ed39aa4afec86c730665 to your computer and use it in GitHub Desktop.
Find if a number is prime in elixir and get a list of primes.
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}) -> | |
cond do | |
!is_prime -> {false, fac} | |
fac > upper_limit -> {is_prime, upper_limit} | |
true -> | |
is_prime = rem(x, fac) != 0 | |
upper_limit = if is_prime, do: div(x, fac + 1), else: fac | |
{is_prime , upper_limit} | |
end | |
end) |> elem(0) | |
end | |
end |
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
defmodule PrimeArray 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?(n) do | |
floored_sqrt = :math.sqrt(n) | |
|> Float.floor | |
|> round | |
!Enum.any?(2..floored_sqrt, &(rem(n, &1) == 0)) | |
end | |
end |
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. | |
# This uses stream to check if a number is prime or not! | |
defmodule NPrimesStream 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 | |
fac_lim = Stream.unfold({2, div(x, 2)}, fn({fac, upper_lim}) -> | |
cond do | |
fac > upper_lim -> nil | |
true -> | |
if rem(x, fac) == 0, do: nil, else: {{fac, upper_lim}, {fac + 1, div(x, fac + 1)}} | |
end | |
end) |> Enum.to_list |> List.last | |
!is_nil(fac_lim) && elem(fac_lim, 0) <= elem(fac_lim, 1) | |
end | |
end | |
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
defmodule StepPrimes do | |
def step(g, m, n) do | |
Stream.unfold({m, []}, fn({x, acc}) -> | |
cond do | |
!acc || x >= n - g -> nil | |
is_prime?(x) && is_prime?(x + g) -> {[x, x + g], {x + 1, nil}} | |
true -> {acc, {x + 1, []}} | |
end | |
end) |> Enum.to_list |> List.last | |
end | |
defp is_prime?(n) when n in [2, 3], do: true | |
defp is_prime?(n) do | |
floored_sqrt = :math.sqrt(n) | |
|> Float.floor | |
|> round | |
!Enum.any?(2..floored_sqrt, &(rem(n, &1) == 0)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment