Python/maths/binary_exponentiation_2.py
Vipin Karthic 6d13603667
Fixes #9943 Added Doctests to binary_exponentiation_3.py (#10121)
* Python mirror_formulae.py is added to the repository

* Changes done after reading readme.md

* Changes for running doctest on all platforms

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Change 2 for Doctests

* Changes for doctest 2

* updating DIRECTORY.md

* Doctest whitespace error rectification to mirror_formulae.py

* updating DIRECTORY.md

* Adding Thermodynamic Work Done Formulae

* Work done on/by body in a thermodynamic setting

* updating DIRECTORY.md

* updating DIRECTORY.md

* Doctest adiition to binary_exponentiation_3.py

* Change 1

* updating DIRECTORY.md

* Rename binary_exponentiation_3.py to binary_exponentiation_2.py

* updating DIRECTORY.md

* updating DIRECTORY.md

* Formatting

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-10-10 01:52:37 -04:00

62 lines
1.3 KiB
Python

"""
Binary Exponentiation
This is a method to find a^b in O(log b) time complexity
This is one of the most commonly used methods of exponentiation
It's also useful when the solution to (a^b) % c is required because a, b, c may be
over the computer's calculation limits
Let's say you need to calculate a ^ b
- RULE 1 : a ^ b = (a*a) ^ (b/2) ---- example : 4 ^ 4 = (4*4) ^ (4/2) = 16 ^ 2
- RULE 2 : IF b is odd, then a ^ b = a * (a ^ (b - 1)), where b - 1 is even
Once b is even, repeat the process until b = 1 or b = 0, because a^1 = a and a^0 = 1
For modular exponentiation, we use the fact that (a*b) % c = ((a%c) * (b%c)) % c
Now apply RULE 1 or 2 as required
@author chinmoy159
"""
def b_expo(a: int, b: int) -> int:
"""
>>> b_expo(2, 10)
1024
>>> b_expo(9, 0)
1
>>> b_expo(0, 12)
0
>>> b_expo(4, 12)
16777216
"""
res = 1
while b > 0:
if b & 1:
res *= a
a *= a
b >>= 1
return res
def b_expo_mod(a: int, b: int, c: int) -> int:
"""
>>> b_expo_mod(2, 10, 1000000007)
1024
>>> b_expo_mod(11, 13, 19)
11
>>> b_expo_mod(0, 19, 20)
0
>>> b_expo_mod(15, 5, 4)
3
"""
res = 1
while b > 0:
if b & 1:
res = ((res % c) * (a % c)) % c
a *= a
b >>= 1
return res