Skip to content

Commit

Permalink
Added doctests.
Browse files Browse the repository at this point in the history
  • Loading branch information
quant12345 committed Oct 7, 2023
1 parent 6143652 commit 92639ba
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions maths/carmichael_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@


def gcd(a: int, b: int) -> int:
"""
Examples:
>>> gcd(9, 3)
3
>>> gcd(2, 1)
1
"""
if a < b:
return gcd(b, a)
if a % b == 0:
Expand All @@ -21,6 +30,15 @@ def gcd(a: int, b: int) -> int:


def power(x: int, y: int, mod: int) -> int:
"""
Examples:
>>> power(2, 15, 3)
2
>>> power(5, 1, 30)
5
"""
if y == 0:
return 1
temp = power(x, y // 2, mod) % mod
Expand All @@ -31,6 +49,20 @@ def power(x: int, y: int, mod: int) -> int:


def is_carmichael_number(n: int) -> bool:
"""
Examples:
>>> is_carmichael_number(562)
False
>>> is_carmichael_number(561)
True
>>> is_carmichael_number(5.1)
Traceback (most recent call last):
...
ValueError: Number 5.1 must instead be integer
"""
b = 2
while b < n:
if gcd(b, n) == 1 and power(b, n - 1, n) != 1:
Expand All @@ -40,6 +72,10 @@ def is_carmichael_number(n: int) -> bool:


if __name__ == "__main__":
import doctest

doctest.testmod()

number = int(input("Enter number: ").strip())
if is_carmichael_number(number):
print(f"{number} is a Carmichael Number.")
Expand Down

0 comments on commit 92639ba

Please sign in to comment.