Skip to content

Instantly share code, notes, and snippets.

@houmanka
Last active March 18, 2020 12:14
Show Gist options
  • Save houmanka/f820b0ef1b34e1af79b460a5cefba234 to your computer and use it in GitHub Desktop.
Save houmanka/f820b0ef1b34e1af79b460a5cefba234 to your computer and use it in GitHub Desktop.
Crude Recursion
"""
238 - 128 = 110
110 - 64 = 46
46 - 32 = 14
14 - 8 = 6
6 - 4 = 2
2 - 2 = 0
"""
defmodule AncientEgyptianMultiplication do
require Integer
def of(0.0, state), do: state
def of(238.0, state) do
of(238 - :math.pow(2, 7), concat( state, :math.pow(2, 7) ))
end
def of(110.0, state) do
of(110 - :math.pow(2, 6), concat( state, :math.pow(2, 6) ))
end
def of(46.0, state) do
of(46 - :math.pow(2, 5), concat(state, :math.pow(2, 5)))
end
def of(14.0, state) do
of(14 - :math.pow(2, 3), concat(state, :math.pow(2, 3)))
end
def of(6.0, state) do
of(6 - :math.pow(2, 2), concat(state, :math.pow(2, 2)))
end
def of(2.0, state) do
of(2 - :math.pow(2, 1), concat(state, :math.pow(2, 1)))
end
def of(_, _L), do: "Incorrect Input"
defp concat(first, second) do
Enum.concat(first, [second])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment