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 | |
| 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 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 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 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 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 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 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.IncrementTest do | |
| use ExUnit.Case | |
| doctest RussianPeasantMultiplication.Increment | |
| import Monad.Result | |
| alias RussianPeasantMultiplication.Increment, as: RPM_Inc | |
| test "max number must be a number" do | |
| items = [ %{requested: "238", max_round: "4"}, | |
| %{requested: 238, max_round: "4"}, |
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.CombineTest do | |
| use ExUnit.Case | |
| doctest RussianPeasantMultiplication.Combine | |
| import Monad.Result | |
| alias RussianPeasantMultiplication.Combine, as: Combine | |
| test "needs to create tuple from 2 lists" do | |
| res = Combine.combine([13, 6, 3, 1], [238, 476, 952, 1904]) |> unwrap! |