Skip to content
Merged
Changes from 5 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,31 @@ def b_expo(a: int, b: int) -> int:


def b_expo_mod(a: int, b: int, c: int) -> int:
"""
Calculate (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)
11
>>> b_expo_mod(2, 1, 5)
2
>>> b_expo_mod(1, 10, 3)
1
"""
res = 0
while b > 0:
if b & 1:
Expand All @@ -35,6 +86,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