Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added doctests
  • Loading branch information
quant12345 committed Oct 7, 2023
commit 0e0c707e9759a4184e6f57319fd691b27b4a1a36
39 changes: 39 additions & 0 deletions maths/carmichael_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@


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 +31,16 @@ 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 +51,25 @@ 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
"""

if not isinstance(n, int):
msg = f"Number {n} must instead be integer"
raise ValueError(msg)

b = 2
while b < n:
if gcd(b, n) == 1 and power(b, n - 1, n) != 1:
Expand Down