From 0c1bcfcbeed15b0f755a71edf9e2da47a8379067 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 18 Nov 2020 11:38:57 +0100 Subject: [PATCH] Add recursive prime number generation Signed-off-by: trivernis --- .formatter.exs | 4 ++++ .gitignore | 29 +++++++++++++++++++++++++++++ README.md | 21 +++++++++++++++++++++ elixir_primes.iml | 19 +++++++++++++++++++ lib/primes.ex | 22 ++++++++++++++++++++++ mix.exs | 29 +++++++++++++++++++++++++++++ test/hello_world_test.exs | 8 ++++++++ test/test_helper.exs | 1 + 8 files changed, 133 insertions(+) create mode 100644 .formatter.exs create mode 100644 .gitignore create mode 100644 README.md create mode 100644 elixir_primes.iml create mode 100644 lib/primes.ex create mode 100644 mix.exs create mode 100644 test/hello_world_test.exs create mode 100644 test/test_helper.exs diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ec2bce --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +hello_world-*.tar + + +# Temporary files for e.g. tests +/tmp + +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e28644f --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Elixir Primes + +Multithreaded calculation of prime numbers + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed +by adding `primes` to your list of dependencies in `mix.exs`: + +```elixir +def deps do + [ + {:primes, "~> 0.1.0"} + ] +end +``` + +Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) +and published on [HexDocs](https://hexdocs.pm). Once published, the docs can +be found at [https://hexdocs.pm/hello_world](https://hexdocs.pm/hello_world). + diff --git a/elixir_primes.iml b/elixir_primes.iml new file mode 100644 index 0000000..81d8d1b --- /dev/null +++ b/elixir_primes.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/primes.ex b/lib/primes.ex new file mode 100644 index 0000000..8240d29 --- /dev/null +++ b/lib/primes.ex @@ -0,0 +1,22 @@ +defmodule Primes do + def start(_type, _args) do + Enum.each(0..6, fn (n) -> Task.start(fn -> primes(2*n + 1, 16) end); end) + + primes(15, 16) + end + + defp primes(value, step) do + if rem(value, 2) != 0 && check_prime(value, 3) do + IO.puts(value) + end + primes(value + step, step) + end + + defp check_prime(value, num) do + cond do + num > :math.sqrt(value) -> true + rem(value, num) != 0 -> check_prime(value, num + 2) + true -> false + end + end +end diff --git a/mix.exs b/mix.exs new file mode 100644 index 0000000..6706b20 --- /dev/null +++ b/mix.exs @@ -0,0 +1,29 @@ +defmodule HelloWorld.MixProject do + use Mix.Project + + def project do + [ + app: :primes, + version: "0.1.0", + elixir: "~> 1.11", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + mod: {Primes, []}, + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/test/hello_world_test.exs b/test/hello_world_test.exs new file mode 100644 index 0000000..bc4508b --- /dev/null +++ b/test/hello_world_test.exs @@ -0,0 +1,8 @@ +defmodule HelloWorldTest do + use ExUnit.Case + doctest HelloWorld + + test "greets the world" do + assert HelloWorld.hello() == :world + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()