Skip to content

Instantly share code, notes, and snippets.

@Radcliffe
Created August 23, 2018 01:55
Show Gist options
  • Save Radcliffe/4f530f6f25e51529a66878cc68582f20 to your computer and use it in GitHub Desktop.
Save Radcliffe/4f530f6f25e51529a66878cc68582f20 to your computer and use it in GitHub Desktop.
Fast exponentiation in Erlang
-module(power).
-export([pow/2]).
pow(A, B) -> pow(A, B, 1).
pow(_, 0, C) -> C;
pow(A, B, C) when B rem 2 == 1 -> pow(A, B-1, A*C);
pow(A, B, C) -> pow(A*A, B div 2, C).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment