Skip to content

Instantly share code, notes, and snippets.

@houmanka
Created March 19, 2020 09:02
Show Gist options
  • Save houmanka/aefc6db4aab57c0c5bd62115ab5a16dd to your computer and use it in GitHub Desktop.
Save houmanka/aefc6db4aab57c0c5bd62115ab5a16dd to your computer and use it in GitHub Desktop.
A bit more intelligent
defmodule AncientEgyptianMultiplication do
require Integer
def of(n, _, state) when n <= 1, do: state
def of(n, closest_number, state) do
pow_2 = :math.pow(2, closest_number)
case pow_2 < n do
true ->
state = state ++ [pow_2]
n = n - pow_2
of(n, closest_number - 1, state)
false ->
of(n, closest_number - 1, state)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment