Skip to content

maths/binary_exponentiation_2.py Renames and added doctests #9499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@
* [Basic Maths](maths/basic_maths.py)
* [Binary Exp Mod](maths/binary_exp_mod.py)
* [Binary Exponentiation](maths/binary_exponentiation.py)
* [Binary Exponentiation 2](maths/binary_exponentiation_2.py)
* [Binary Exponentiation 2](maths/binary_multiplication.py)
* [Binary Exponentiation 3](maths/binary_exponentiation_3.py)
* [Binomial Coefficient](maths/binomial_coefficient.py)
* [Binomial Distribution](maths/binomial_distribution.py)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@


def b_expo(a: int, b: int) -> int:
"""
Calculate the result of multiplying 'a' and 'b' using bitwise multiplication.

Parameters:
a (int): The first number.
b (int): The second number.

Returns:
int: The result of 'a' multiplied by 'b'.

Examples:
>>> b_expo(2, 3)
6
>>> b_expo(5, 0)
0
>>> b_expo(3, 4)
12
>>> b_expo(10, 5)
50
>>> b_expo(0, 5)
0
>>> b_expo(2, 1)
2
>>> b_expo(1, 10)
10
"""
res = 0
while b > 0:
if b & 1:
Expand All @@ -23,6 +49,12 @@ def b_expo(a: int, b: int) -> int:
return res


if __name__ == "__main__":
import doctest

doctest.testmod()


def b_expo_mod(a: int, b: int, c: int) -> int:
res = 0
while b > 0:
Expand Down