Created
August 23, 2018 01:55
-
-
Save Radcliffe/4f530f6f25e51529a66878cc68582f20 to your computer and use it in GitHub Desktop.
Fast exponentiation in Erlang
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
-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