commit 65df630

Artur Manuel  ·  2026-03-21 17:41:54 +0000 UTC
parent c1b49c7
feat(elixir): add elixir
7 files changed,  +127, -0
+4, -0
1@@ -0,0 +1,4 @@
2+# Used by "mix format"
3+[
4+  inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
5+]
+26, -0
 1@@ -0,0 +1,26 @@
 2+# The directory Mix will write compiled artifacts to.
 3+/_build/
 4+
 5+# If you run "mix test --cover", coverage assets end up here.
 6+/cover/
 7+
 8+# The directory Mix downloads your dependencies sources to.
 9+/deps/
10+
11+# Where third-party dependencies like ExDoc output generated docs.
12+/doc/
13+
14+# If the VM crashes, it generates a dump, let's ignore it too.
15+erl_crash.dump
16+
17+# Also ignore archive artifacts (built via "mix archive.build").
18+*.ez
19+
20+# Ignore package tarball (built via "mix hex.build").
21+collatz_conjecture-*.tar
22+
23+# Temporary files, for example, from tests.
24+/tmp/
25+
26+# Executable
27+collatz_conjecture
+8, -0
1@@ -0,0 +1,8 @@
2+defmodule CollatzConjecture.CLI do
3+  def main(_args \\ []) do
4+    1..10000
5+    |> Enum.map(fn x -> CollatzConjecture.collatz_sequence(x) end)
6+    |> Enum.join("\n")
7+    |> IO.puts()
8+  end
9+end
+56, -0
 1@@ -0,0 +1,56 @@
 2+defmodule CollatzConjecture do
 3+  @moduledoc """
 4+  Documentation for `CollatzConjecture`.
 5+  """
 6+
 7+  require Integer
 8+
 9+  @doc """
10+    Returns the next collatz term of the given `n`.
11+
12+    If `n` is even, return `n` divided by 2.
13+    Otherwise, return 3 multiplied by `n`, plus 1.
14+  """
15+  @spec collatz(integer) :: integer
16+  def collatz(n) when Integer.is_even(n) do
17+    div(n, 2)
18+  end
19+
20+  def collatz(n) do
21+    3 * n + 1
22+  end
23+
24+  @doc """
25+    Returns a string containing the sequence of Collatz terms starting
26+    from `n`.
27+
28+    During the :first iteration, there will be result in `n: ...`.
29+    However, :continuing iterations will result in `n, ...`.
30+    If `n` is 1 during :continuing iterations, the result will be
31+    "1".
32+  """
33+  @spec collatz_sequence(integer, :first | :continuing) :: String.t()
34+  def collatz_sequence(n, :first) do
35+    "#{n}: " <> collatz_sequence(collatz(n), :continuing)
36+  end
37+
38+  def collatz_sequence(n, :continuing) when n == 1 do
39+    "1"
40+  end
41+
42+  def collatz_sequence(n, :continuing) do
43+    "#{n}, " <> collatz_sequence(collatz(n), :continuing)
44+  end
45+
46+  @doc """
47+    Returns a string containing the sequence of Collatz terms starting
48+    from `n`.
49+
50+    This is an abstraction for collatz_sequence/2 which always starts
51+    with `:first`.
52+  """
53+  @spec collatz_sequence(integer) :: String.t()
54+  def collatz_sequence(n) do
55+    collatz_sequence(n, :first)
56+  end
57+end
+18, -0
 1@@ -0,0 +1,18 @@
 2+defmodule CollatzConjecture.MixProject do
 3+  use Mix.Project
 4+
 5+  def project do
 6+    [
 7+      app: :collatz_conjecture,
 8+      version: "0.1.0",
 9+      elixir: "~> 1.18",
10+      start_permanent: Mix.env() == :prod,
11+      escript: escript()
12+    ]
13+  end
14+
15+  # Run "mix help compile.app" to learn about applications.
16+  def escript do
17+    [main_module: CollatzConjecture.CLI]
18+  end
19+end
+14, -0
 1@@ -0,0 +1,14 @@
 2+defmodule CollatzConjectureTest do
 3+  use ExUnit.Case
 4+  doctest CollatzConjecture
 5+
 6+  test "returns collatz terms" do
 7+    assert CollatzConjecture.collatz(1) == 4
 8+    assert CollatzConjecture.collatz(4) == 2
 9+  end
10+
11+  test "returns collatz sequences" do
12+    assert CollatzConjecture.collatz_sequence(1, :first) == "1: 4, 2, 1"
13+    assert CollatzConjecture.collatz_sequence(4, :first) == "4: 2, 1"
14+  end
15+end
+1, -0
1@@ -0,0 +1 @@
2+ExUnit.start()