Last active
February 23, 2016 21:50
-
-
Save mhammiche/6dd6d3f3de207662103d to your computer and use it in GitHub Desktop.
Elixir implementation of russian peasant algorithm
This file contains 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 Russian do | |
require Integer | |
use Bitwise | |
@doc ~S""" | |
Multiply two integer using the Russian Peasant Algorithm | |
## Examples | |
iex> Russian.mult(5,3) | |
15 | |
""" | |
def mult(a, 0), do: 0 | |
def mult(a, 1), do: a | |
def mult(a, b) when Integer.is_odd(b), do: | |
a + mult(a <<< 1, b >>> 1) | |
def mult(a, b), do: | |
mult(a <<< 1, b >>> 1) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment