From 839c7e56c4915c24220089caba1e46de2edc4474 Mon Sep 17 00:00:00 2001 From: Kyle Lobo Date: Sat, 25 May 2019 11:30:31 +0530 Subject: [PATCH] Added explanation for O(logn) solution in "Exponentiation" (#28807) --- guide/english/algorithms/exponentiation/index.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/guide/english/algorithms/exponentiation/index.md b/guide/english/algorithms/exponentiation/index.md index c32ca84488..32f9dc63e8 100644 --- a/guide/english/algorithms/exponentiation/index.md +++ b/guide/english/algorithms/exponentiation/index.md @@ -35,6 +35,14 @@ int power(int x, unsigned int y) { return x*temp*temp; } ``` +Why is this faster? + +Suppose we have x = 5, y = 4, we know that our answer is going to be (5 * 5 * 5 * 5). + +If we break this down, we notice that we can write (5 * 5 * 5 * 5) as (5 * 5) * 2 and further, we can write (5 * 5) as 5 * 2. + +Through this observation, we can optimize our function to O(log n) by calculating power(x, y/2) only once and storing it. + ## Modular Exponentiation @@ -49,7 +57,7 @@ int power(int x, unsigned int y, int p) { res = (res*x) % p; // y must be even now - y = y>>1; + y = y >> 1; x = (x*x) % p; } return res;