Skip to content

add tests to binary exponentiation #9947

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
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
50 changes: 42 additions & 8 deletions maths/binary_exponentiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,45 @@


def binary_exponentiation(a: int, n: int) -> int:
"""

>>> binary_exponentiation(1, 1)
1

>>> binary_exponentiation(2, 1)
2

>>> binary_exponentiation(3, 1)
3

>>> binary_exponentiation(4, 1)
4

>>> binary_exponentiation(1, 2)
1

>>> binary_exponentiation(2, 2)
4

>>> binary_exponentiation(3, 2)
9

>>> binary_exponentiation(4, 2)
16

>>> binary_exponentiation(1, 3)
1

>>> binary_exponentiation(2, 3)
8

>>> binary_exponentiation(3, 3)
27

>>> binary_exponentiation(4, 3)
64

"""
if n == 0:
return 1

Expand All @@ -17,11 +56,6 @@ def binary_exponentiation(a: int, n: int) -> int:


if __name__ == "__main__":
try:
BASE = int(input("Enter Base : ").strip())
POWER = int(input("Enter Power : ").strip())
except ValueError:
print("Invalid literal for integer")

RESULT = binary_exponentiation(BASE, POWER)
print(f"{BASE}^({POWER}) : {RESULT}")
import doctest

doctest.testmod()
120 changes: 120 additions & 0 deletions maths/binary_exponentiation_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,45 @@


def b_expo(a: int, b: int) -> int:
"""

>>> b_expo(1, 1)
1

>>> b_expo(2, 1)
2

>>> b_expo(3, 1)
3

>>> b_expo(4, 1)
4

>>> b_expo(1, 2)
1

>>> b_expo(2, 2)
4

>>> b_expo(3, 2)
9

>>> b_expo(4, 2)
16

>>> b_expo(1, 3)
1

>>> b_expo(2, 3)
8

>>> b_expo(3, 3)
27

>>> b_expo(4, 3)
64

"""
res = 1
while b > 0:
if b & 1:
Expand All @@ -24,6 +63,81 @@ def b_expo(a: int, b: int) -> int:


def b_expo_mod(a: int, b: int, c: int) -> int:
"""

>>> b_expo_mod(1, 1, 7)
1

>>> b_expo_mod(2, 1, 7)
2

>>> b_expo_mod(3, 1, 7)
3

>>> b_expo_mod(4, 1, 7)
4

>>> b_expo_mod(1, 2, 7)
1

>>> b_expo_mod(2, 2, 7)
4

>>> b_expo_mod(3, 2, 7)
2

>>> b_expo_mod(4, 2, 7)
2

>>> b_expo_mod(1, 3, 7)
1

>>> b_expo_mod(2, 3, 7)
1

>>> b_expo_mod(3, 3, 7)
6

>>> b_expo_mod(4, 3, 7)
1

>>> b_expo_mod(1, 1, 5)
1

>>> b_expo_mod(2, 1, 5)
2

>>> b_expo_mod(3, 1, 5)
3

>>> b_expo_mod(4, 1, 5)
4

>>> b_expo_mod(1, 2, 5)
1

>>> b_expo_mod(2, 2, 5)
4

>>> b_expo_mod(3, 2, 5)
4

>>> b_expo_mod(4, 2, 5)
1

>>> b_expo_mod(1, 3, 5)
1

>>> b_expo_mod(2, 3, 5)
3

>>> b_expo_mod(3, 3, 5)
2

>>> b_expo_mod(4, 3, 5)
4

"""
res = 1
while b > 0:
if b & 1:
Expand All @@ -48,3 +162,9 @@ def b_expo_mod(a: int, b: int, c: int) -> int:
* the fact : (a*b) % c = ((a%c) * (b%c)) % c
* Now apply RULE 1 OR 2 whichever is required.
"""


if __name__ == "__main__":
import doctest

doctest.testmod()
50 changes: 45 additions & 5 deletions maths/carmichael_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ def gcd(a: int, b: int) -> int:


def power(x: int, y: int, mod: int) -> int:
"""
>>> power(1,2,3)
1


>>> power(2,4,7)
2

>>> power(4,2,9)
7
"""
if y == 0:
return 1
temp = power(x, y // 2, mod) % mod
Expand All @@ -31,6 +42,37 @@ def power(x: int, y: int, mod: int) -> int:


def is_carmichael_number(n: int) -> bool:
"""
>>> is_carmichael_number(1)
True

>>> is_carmichael_number(2)
True

>>> is_carmichael_number(8)
False

>>> is_carmichael_number(245)
False

>>> is_carmichael_number(561)
True

>>> is_carmichael_number(1105)
True

>>> is_carmichael_number(1729)
True

>>> is_carmichael_number(1728)
False

>>> is_carmichael_number(8910)
False

>>> is_carmichael_number(8911)
True
"""
b = 2
while b < n:
if gcd(b, n) == 1 and power(b, n - 1, n) != 1:
Expand All @@ -40,8 +82,6 @@ def is_carmichael_number(n: int) -> bool:


if __name__ == "__main__":
number = int(input("Enter number: ").strip())
if is_carmichael_number(number):
print(f"{number} is a Carmichael Number.")
else:
print(f"{number} is not a Carmichael Number.")
import doctest

doctest.testmod()