Skip to content

Instantly share code, notes, and snippets.

@mhammiche
Last active February 23, 2016 21:50
Show Gist options
  • Save mhammiche/6dd6d3f3de207662103d to your computer and use it in GitHub Desktop.
Save mhammiche/6dd6d3f3de207662103d to your computer and use it in GitHub Desktop.
Elixir implementation of russian peasant algorithm
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