about summary refs log blame commit diff
path: root/users/wpcarro/assessments/semiprimes/server/lib/server.ex
blob: 7ab5e905b5a062956dff4117d1bee2c08a4e2303 (plain) (tree)
1
2
3
4
5
6





                             








                                                                        
 

                             
                        
           
 
            
                   

       
 




                          

     
defmodule Server do
  @moduledoc """
  Documentation for `Server`.
  """

  @doc """
  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

          res ->
            Cache.put(n, res)
            {:miss, res}
        end

      hit ->
        {:hit, hit}
    end
  end

  defp do_semiprime(n) do
    case Math.factor(n) do
      [_, _] = res -> res
      _ -> nil
    end
  end
end