diff --git a/maths/binary_exponentiation.py b/maths/binary_exponentiation.py index 05de939d1bde..115de127e5e8 100644 --- a/maths/binary_exponentiation.py +++ b/maths/binary_exponentiation.py @@ -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 @@ -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() diff --git a/maths/binary_exponentiation_3.py b/maths/binary_exponentiation_3.py index 9cd143e09207..9a99f3af1cf4 100644 --- a/maths/binary_exponentiation_3.py +++ b/maths/binary_exponentiation_3.py @@ -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: @@ -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: @@ -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() diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index c9c144759246..fdfcd8d77242 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -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 @@ -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: @@ -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()