Skip to content

Commit ee810e4

Browse files
authored
Merge pull request cp-algorithms#111 from gabrielsimoes/binexpfixes
Improvements in Binary Exponentiation
2 parents c3f0249 + 1c02801 commit ee810e4

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

src/algebra/binary-exp.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ $$3^{11} = 3^{1011_{2}} = 3^{8} \cdot 3^{2} \cdot 3^{1}$$
1515

1616
So we need only find sequence of squared numbers:
1717

18-
$3^{1} = 3$
19-
$3^{2} = (3^{1})^{2} = 3 \cdot 3 = 9$
20-
$3^{4} = (3^{2})^{2} = 9 \cdot 9 = 81$
18+
$3^{1} = 3$
19+
$3^{2} = (3^{1})^{2} = 3 \cdot 3 = 9$
20+
$3^{4} = (3^{2})^{2} = 9 \cdot 9 = 81$
2121
$3^{8} = (3^{4})^{2} = 81 \cdot 81 = 6561$
2222

23-
And multiply three of them (skipping $3^{4}$ because corresponding bit in `b` is zero):
23+
And multiply three of them (skipping `3^{4}` because corresponding bit in `b` is zero):
2424
$\quad 3^{11} = 6561 \cdot 9 \cdot 3 = 177147$
2525

2626
This approach has important applications in many tasks not related to arithmetics, since it
@@ -36,7 +36,7 @@ and it is still used inside CPUs of contemporary computers!
3636

3737
###Algorithm
3838

39-
The approach described above could be easily implemented with a single loop.
39+
The approach described above could be easily implemented with a single loop.
4040

4141
**Non-recursive** approach in Python 3 <span class="toggle-code">Show/Hide</span>:
4242

@@ -55,18 +55,18 @@ Approach in C++ <span class="toggle-code">Show/Hide</span>:
5555
long long binpow(long long a,long long b)
5656
{
5757
long long res = 1;
58-
while(b){
59-
if(b&1) res = res*a;
60-
a = (a*a);
61-
b >>=1;
58+
while (b){
59+
if (b & 1) res = res * a;
60+
a = (a * a);
61+
b >>= 1;
6262
}
6363
return res;
6464
}
6565

6666
This approach builds the result starting from smallest degrees of `a`. If we use recursion
6767
instead of loop we can work in "inverse" direction, starting from largest degrees and dividing
6868
`b` in two at each step.
69-
69+
7070
**Recursive** approach in Python 3 <span class="toggle-code">Show/Hide</span>:
7171

7272
def binpow(a, b):
@@ -75,19 +75,19 @@ instead of loop we can work in "inverse" direction, starting from largest degree
7575
res = binpow(a, b // 2)
7676
return res * res * (a if b % 2 != 0 else 1)
7777

78-
Approach in c++<span class="toggle-code">Show/Hide</span>:
78+
Approach in C++ <span class="toggle-code">Show/Hide</span>:
7979

8080
long long binpow(long long a,long long b)
8181
{
82-
if(b==1) return a;
83-
long long res = binpow(a,b/2);
84-
if(b%2)return res*res*a;
85-
else return res*res;
82+
if (b == 1) return a;
83+
long long res = binpow(a, b/2);
84+
if (b % 2) return res*res*a;
85+
else return res * res;
8686
}
8787

88-
We can explain this last approach mathematically:
89-
$a^{b} = (a^{b/2})^2 \quad$ for even `b`,
90-
$a^{b} = (a^{(b-1)/2})^2 \cdot a \quad$ for odd `b`,
88+
We can explain this last approach mathematically:
89+
$a^{b} = (a^{b/2})^2 \quad$ for even `b`,
90+
$a^{b} = (a^{(b-1)/2})^2 \cdot a \quad$ for odd `b`,
9191
$a^{1} = a$.
9292

9393
## Practice Problems

0 commit comments

Comments
 (0)