2020-12-11 23:42:16 +01:00
|
|
|
defmodule Server do
|
|
|
|
@moduledoc """
|
|
|
|
Documentation for `Server`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@doc """
|
2020-12-12 02:32:31 +01:00
|
|
|
If `n` contains exactly two prime factors, return those prime factors;
|
|
|
|
otherwise, return nothing.
|
|
|
|
"""
|
|
|
|
def semiprime(n) do
|
|
|
|
case Cache.get(n) do
|
|
|
|
nil ->
|
|
|
|
case do_semiprime(n) do
|
|
|
|
nil ->
|
|
|
|
nil
|
2020-12-11 23:42:16 +01:00
|
|
|
|
2020-12-12 02:32:31 +01:00
|
|
|
res ->
|
|
|
|
Cache.put(n, res)
|
2020-12-12 03:41:42 +01:00
|
|
|
{:miss, res}
|
2020-12-12 02:32:31 +01:00
|
|
|
end
|
2020-12-11 23:42:16 +01:00
|
|
|
|
2020-12-12 02:32:31 +01:00
|
|
|
hit ->
|
2020-12-12 03:41:42 +01:00
|
|
|
{:hit, hit}
|
2020-12-12 02:32:31 +01:00
|
|
|
end
|
|
|
|
end
|
2020-12-11 23:42:16 +01:00
|
|
|
|
2020-12-12 02:32:31 +01:00
|
|
|
defp do_semiprime(n) do
|
|
|
|
case Math.factor(n) do
|
|
|
|
[_, _] = res -> res
|
|
|
|
_ -> nil
|
|
|
|
end
|
2020-12-11 23:42:16 +01:00
|
|
|
end
|
|
|
|
end
|