Skip to content

Changing Name of file and adding doctests in file. #9513

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

Merged
merged 13 commits into from
Oct 3, 2023
Merged
Changes from 2 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
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 @@ -24,6 +50,35 @@ def b_expo(a: int, b: int) -> int:


def b_expo_mod(a: int, b: int, c: int) -> int:
"""
Calculate the result of (a * b) % c using binary exponentiation and modular arithmetic.

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

Returns:
int: The result of (a * b) % c.

Examples:
>>> b_expo_mod(2, 3, 5)
1
>>> b_expo_mod(5, 0, 7)
0
>>> b_expo_mod(3, 4, 6)
0
>>> b_expo_mod(10, 5, 13)
8
>>> b_expo_mod(2, 1, 5)
2
>>> b_expo_mod(1, 10, 3)
1
>>> b_expo_mod(7, 3, 4)
1
>>> b_expo_mod(8, 2, 10)
6
"""
res = 0
while b > 0:
if b & 1:
Expand All @@ -35,6 +90,11 @@ def b_expo_mod(a: int, b: int, c: int) -> int:
return res


if __name__ == "__main__":
import doctest

doctest.testmod()

"""
* Wondering how this method works !
* It's pretty simple.
Expand Down