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>
This commit is contained in:
Vipin Karthic
2023-10-10 11:22:37 +05:30
committed by GitHub
parent f3acb52cad
commit 6d13603667
2 changed files with 58 additions and 25 deletions

View File

@ -1,17 +1,33 @@
"""
* Binary Exponentiation for Powers
* This is a method to find a^b in a time complexity of O(log b)
* This is one of the most commonly used methods of finding powers.
* Also useful in cases where solution to (a^b)%c is required,
* where a,b,c can be numbers over the computers calculation limits.
* Done using iteration, can also be done using recursion
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
* @author chinmoy159
* @version 1.0 dated 10/08/2017
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:
@ -24,6 +40,16 @@ def b_expo(a: int, b: int) -> int:
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:
@ -33,18 +59,3 @@ def b_expo_mod(a: int, b: int, c: int) -> int:
b >>= 1
return res
"""
* Wondering how this method works !
* It's pretty simple.
* 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 to get a ^ b
* Repeat the process till b = 1 OR b = 0, because a^1 = a AND a^0 = 1
*
* As far as the modulo is concerned,
* the fact : (a*b) % c = ((a%c) * (b%c)) % c
* Now apply RULE 1 OR 2 whichever is required.
"""