Skip to content

Latest commit

 

History

History
63 lines (44 loc) · 1.14 KB

008-decbin.livemd

File metadata and controls

63 lines (44 loc) · 1.14 KB

8 - Decimal a Binario

DECIMAL A BINARIO

Enunciado

Crea un programa se encargue de transformar un número decimal a binario sin utilizar funciones propias del lenguaje que lo hagan directamente.

Solución

# See https://www.geeksforgeeks.org/decimal-binary-number-using-recursion/
defmodule Convert do
  def binary(dec) when dec == 0 do
    0
  end

  def binary(dec) do
    rem(dec, 2) + 10 * binary(div(dec, 2))
  end
end
{:module, Convert, <<70, 79, 82, 49, 0, 0, 6, ...>>, {:binary, 1}}
defmodule Solution do
  def run(number) do
    Convert.binary(number)
  end
end
{:module, Solution, <<70, 79, 82, 49, 0, 0, 6, ...>>, {:run, 1}}
Solution.run(420) == 110_100_100
true