Last active
March 18, 2020 12:14
-
-
Save houmanka/f820b0ef1b34e1af79b460a5cefba234 to your computer and use it in GitHub Desktop.
Crude Recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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