Skip to content

Instantly share code, notes, and snippets.

@houmanka
Created March 19, 2020 22:58
Show Gist options
  • Save houmanka/087f93d0270399d49ede1f6f9007aebc to your computer and use it in GitHub Desktop.
Save houmanka/087f93d0270399d49ede1f6f9007aebc to your computer and use it in GitHub Desktop.
AEM multiplication
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