Prime Number in Elixir

Published on 18 June 2026 (Updated: 18 June 2026)

Welcome to the Prime Number in Elixir page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

defmodule PrimeNumber do
  @doc """
  Determines whether `n` is a prime number.
  """
  @spec prime?(n :: integer()) :: bool()
  def prime?(0), do: false
  def prime?(1), do: false

  def prime?(n) do
    high = trunc(:math.sqrt(n))

    if high < 2 do
      true
    else
      Enum.all?(2..high, &(rem(n, &1) != 0))
    end
  end
end

with [arg] <- System.argv(),
     {n, ""} when n >= 0 <- Integer.parse(arg) do
  IO.puts(if PrimeNumber.prime?(n), do: "prime", else: "composite")
else
  _ -> IO.puts("Usage: please input a non-negative integer")
end

Prime Number in Elixir was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.