Created
March 19, 2020 22:58
-
-
Save houmanka/087f93d0270399d49ede1f6f9007aebc to your computer and use it in GitHub Desktop.
AEM multiplication
This file contains hidden or 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
defmodule AncientEgyptianMultiplication do | |
require Integer | |
def multiply(first, second) do | |
decomposed = decompose(first) | |
multiply(decomposed, second, []) | |
end | |
def multiply([], _, state), do: sum_of(state) | |
# 128 × 13 + 64 × 13 + 32 × 13 + 8 × 13 + 4 × 13 + 2 × 13 | |
def multiply([head | tail], second, state) do | |
state = state ++ [head * second] | |
multiply(tail, second, state) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment