Elixir Bracket
defmodule Bracket do
def bracket(s) do
result =
s
|> String.graphemes()
|> Enum.reduce_while([], fn ch, stack ->
case ch do
")" -> pop_match(stack, "(")
"]" -> pop_match(stack, "[")
"}" -> pop_match(stack, "{")
_ -> {:cont, [ch | stack]}
end
end)
if result == [], do: 1, else: 0
end
defp pop_match([], _expected), do: {:halt, :mismatch}
defp pop_match([top | rest], expected) do
if top == expected, do: {:cont, rest}, else: {:halt, :mismatch}
end
end
This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.
Elixir Century From Year
defmodule CenturyFromYear do
def century_from_year(year), do: ceil(year / 100)
end
This converts a year into its century. Years 1-100 are century 1, 101-200 are century 2, and so on.
Elixir Check Palindrome
defmodule CheckPalindrome do
def check_palindrome(input_string), do: String.reverse(input_string) == input_string
end
This compares the string with its reverse. If they match, it is a palindrome.
Elixir Chocolates By Numbers
defmodule ChocolatesByNumbers do
def chocolates_by_numbers(n, m) do
g = gcd(n, m)
n * m |> div(g) |> div(m)
end
defp gcd(n, m) when rem(n, m) == 0, do: m
defp gcd(n, m), do: gcd(m, rem(n, m))
end
This uses the greatest common divisor to figure out how many chocolates get eaten before the pattern repeats.
Elixir Common Prime Divisors
defmodule CommonPrimeDivisors do
def common_prime_divisors(a, b) do
a
|> Enum.zip(b)
|> Enum.count(fn {x, y} ->
d = gcd(x, y)
remove_common(x, d) == 1 and remove_common(y, d) == 1
end)
end
defp gcd(n, m) when rem(n, m) == 0, do: m
defp gcd(n, m), do: gcd(m, rem(n, m))
defp remove_common(1, _m), do: 1
defp remove_common(n, m) do
d = gcd(n, m)
if d == 1, do: n, else: remove_common(div(n, d), m)
end
end
This checks whether two numbers are built from the same prime factors by repeatedly dividing out their shared parts.
Elixir Count Div
defmodule CountDiv do
def count_div(a, b, k) do
first_div = if rem(a, k) == 0, do: a, else: a + (k - rem(a, k))
last_div = b - rem(b, k)
div(last_div - first_div, k) + 1
end
end
This counts how many numbers in a range are divisible by K without looping through every value.
Elixir Count Factors
defmodule CountFactors do
def count_factors(n), do: loop(1, 0, n)
defp loop(i, count, n) when i * i < n do
count = if rem(n, i) == 0, do: count + 2, else: count
loop(i + 1, count, n)
end
defp loop(i, count, n) do
if i * i == n, do: count + 1, else: count
end
end
This checks divisors in pairs up to the square root, which keeps the work much smaller than testing every number.
Elixir Count Non Divisible
defmodule CountNonDivisible do
def count_non_divisible(a) do
size = length(a)
occurrences = Enum.frequencies(a)
Enum.map(a, fn v -> size - divisor_occurrences(1, v, occurrences, 0) end)
end
defp divisor_occurrences(i, v, _occurrences, count) when i * i > v, do: count
defp divisor_occurrences(i, v, occurrences, count) do
count =
if rem(v, i) == 0 do
count = count + Map.get(occurrences, i, 0)
if div(v, i) != i do
count + Map.get(occurrences, div(v, i), 0)
else
count
end
else
count
end
divisor_occurrences(i + 1, v, occurrences, count)
end
end
This counts how often each value appears, then subtracts the divisor matches so you get the non-divisible count for each item.
Elixir Count Semi Primes
defmodule CountSemiPrimes do
def count_semi_primes(n, p, q) do
primes = sieve(n)
semi_flags = semi_prime_flags(n, primes)
prefix = prefix_sums(semi_flags, n)
p
|> Enum.zip(q)
|> Enum.map(fn {pi, qi} -> Map.get(prefix, qi) - Map.get(prefix, pi - 1, 0) end)
end
defp sieve(n) do
initial = for i <- 0..n, into: %{}, do: {i, i >= 2}
limit = :math.sqrt(n) |> trunc()
Enum.reduce(2..limit//1, initial, fn i, primes ->
if Map.get(primes, i) do
Enum.reduce(i * i..n//i, primes, fn k, acc -> Map.put(acc, k, false) end)
else
primes
end
end)
end
defp semi_prime_flags(n, primes) do
initial = for i <- 0..n, into: %{}, do: {i, 0}
limit = :math.sqrt(n) |> trunc()
Enum.reduce(2..limit//1, initial, fn k, flags ->
if Map.get(primes, k) do
mark_multiples(k, 2, n, primes, flags)
else
flags
end
end)
end
defp mark_multiples(k, i, n, _primes, flags) when i * k > n, do: flags
defp mark_multiples(k, i, n, primes, flags) do
flags = if Map.get(primes, i), do: Map.put(flags, k * i, 1), else: flags
mark_multiples(k, i + 1, n, primes, flags)
end
defp prefix_sums(flags, n) do
{result, _last} =
Enum.reduce(1..n//1, {%{0 => Map.get(flags, 0, 0)}, Map.get(flags, 0, 0)}, fn i,
{acc, prev} ->
cur = prev + Map.get(flags, i, 0)
{Map.put(acc, i, cur), cur}
end)
result
end
end
This precomputes semiprimes and prefix sums so each range query becomes a quick subtraction.
Elixir Cyclic Rotation
defmodule CyclicRotation do
def cyclic_rotation([], _k), do: []
def cyclic_rotation(a, k) do
size = length(a)
shift = rem(k, size)
{front, back} = Enum.split(a, size - shift)
back ++ front
end
end
This rotates the array to the right by K steps and keeps the wrap-around values in the correct order.