Skip to content

Instantly share code, notes, and snippets.

@MaroShim
Created November 1, 2016 08:04
Show Gist options
  • Save MaroShim/f227a9f1081a5bb8a8ba278690f18305 to your computer and use it in GitHub Desktop.
Save MaroShim/f227a9f1081a5bb8a8ba278690f18305 to your computer and use it in GitHub Desktop.
# largest factor of 600851475143
defmodule Problem3 do
# http://wende.github.io/2015/09/07/Elixir-n-prime-numbers.html
def prime?(x), do: (2..round(:math.sqrt(x)) |> Enum.filter(fn a -> rem(x, a) == 0 end) |> length()) == 0
def solve(num) do
(2 .. round(:math.sqrt(num)))
|> Stream.filter(&(rem(num, &1) == 0))
|> Stream.filter(&(prime?(&1)))
|> Enum.to_list
end
def print do
IO.inspect solve(600851475143)
end
end
Problem3.print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment