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 RussianPeasantMultiplication.Increment do | |
import Monad.Result | |
@errors %{ | |
zero: "Must be greater than zero", | |
number: "Requested number and Max round must be numbers" | |
} | |
@doc """ |
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 RussianPeasantMultiplication.Decrement do | |
import Monad.Result | |
@errors %{ | |
zero: "Must be greater than zero", | |
number_only: "Must be non negetive number" | |
} | |
@doc """ |
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 decompose(n) do | |
power_ready = case Integer.is_odd(n) do | |
true -> n - 1 | |
false -> n | |
end | |
greatest_pow_2 = of(power_ready, []) |> count_of | |
decompose(n, greatest_pow_2, []) |
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 |
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 sum_of([head | tail]) do | |
sum_of(tail, head) | |
end | |
def sum_of([], state), do: state | |
def sum_of([head | tail], state) do | |
sum_of(tail, state + head) |
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 decompose(n) do | |
power_ready = case Integer.is_odd(n) do | |
true -> n - 1 | |
false -> n | |
end | |
greatest_pow_2 = of(power_ready, []) |> count_of | |
decompose(n, greatest_pow_2, []) |
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 | |
def count_of([_head | tail]) do | |
count_of(tail, 1) | |
end | |
def count_of([], state), do: state | |
def count_of([_head | tail], state) do | |
count_of(tail, state + 1) | |
end | |
end |
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 of(n, state) when n <= 1, do: state | |
def of(n, state) do | |
n = (n / 2) | |
|> Kernel.trunc | |
state = state ++ [n] | |
of(n, state) | |
end |
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 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] |
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
""" | |
238 - 128 = 110 | |
110 - 64 = 46 | |
46 - 32 = 14 | |
14 - 8 = 6 | |
6 - 4 = 2 | |
2 - 2 = 0 | |
""" |