Skip to content

Instantly share code, notes, and snippets.

@Radcliffe
Created August 23, 2018 01:44
Show Gist options
  • Save Radcliffe/3fceef030ab9493bd127be594352ae80 to your computer and use it in GitHub Desktop.
Save Radcliffe/3fceef030ab9493bd127be594352ae80 to your computer and use it in GitHub Desktop.
Peasant multiplication in Erlang
-module(multiply).
-export([mul/2]).
mul(A, B) -> mul(A, B, 0).
mul(_, 0, C) -> C;
mul(A, B, C) when B rem 2 == 1 -> mul(A, B-1, A+C);
mul(A, B, C) -> mul(A + A, B div 2, C).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment