Skip to content

Commit 9a5a6c6

Browse files
quant12345pre-commit-ci[bot]tianyizheng02
authored
carmichael_number - add doctests (#10038)
* Replacing the generator with numpy vector operations from lu_decomposition. * Revert "Replacing the generator with numpy vector operations from lu_decomposition." This reverts commit ad217c6. * Added doctests * Update carmichael_number.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update carmichael_number.py I make an empty commit to reset: tests are failing. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update carmichael_number.py Made changes taking into account the addition: from maths.greatest_common_divisor import greatest_common_divisor. Now instead of gcd it is used: greatest_common_divisor. * Update carmichael_number.py * Update carmichael_number.py * Update carmichael_number.py I added a check for 0 and negative numbers in the tests and the code itself. Simplified obtaining the final result. * Update carmichael_number.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update maths/carmichael_number.py Co-authored-by: Tianyi Zheng <[email protected]> * Update carmichael_number.py * Update carmichael_number.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tianyi Zheng <[email protected]>
1 parent 5be5d21 commit 9a5a6c6

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

Diff for: maths/carmichael_number.py

+49-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,21 @@
1010
Examples of Carmichael Numbers: 561, 1105, ...
1111
https://en.wikipedia.org/wiki/Carmichael_number
1212
"""
13+
1314
from maths.greatest_common_divisor import greatest_common_divisor
1415

1516

1617
def power(x: int, y: int, mod: int) -> int:
18+
"""
19+
20+
Examples:
21+
>>> power(2, 15, 3)
22+
2
23+
24+
>>> power(5, 1, 30)
25+
5
26+
"""
27+
1728
if y == 0:
1829
return 1
1930
temp = power(x, y // 2, mod) % mod
@@ -24,15 +35,47 @@ def power(x: int, y: int, mod: int) -> int:
2435

2536

2637
def is_carmichael_number(n: int) -> bool:
27-
b = 2
28-
while b < n:
29-
if greatest_common_divisor(b, n) == 1 and power(b, n - 1, n) != 1:
30-
return False
31-
b += 1
32-
return True
38+
"""
39+
40+
Examples:
41+
>>> is_carmichael_number(562)
42+
False
43+
44+
>>> is_carmichael_number(561)
45+
True
46+
47+
>>> is_carmichael_number(5.1)
48+
Traceback (most recent call last):
49+
...
50+
ValueError: Number 5.1 must instead be a positive integer
51+
52+
>>> is_carmichael_number(-7)
53+
Traceback (most recent call last):
54+
...
55+
ValueError: Number -7 must instead be a positive integer
56+
57+
>>> is_carmichael_number(0)
58+
Traceback (most recent call last):
59+
...
60+
ValueError: Number 0 must instead be a positive integer
61+
"""
62+
63+
if n <= 0 or not isinstance(n, int):
64+
msg = f"Number {n} must instead be a positive integer"
65+
raise ValueError(msg)
66+
67+
return all(
68+
power(b, n - 1, n) == 1
69+
for b in range(2, n)
70+
if greatest_common_divisor(b, n) == 1
71+
)
3372

3473

3574
if __name__ == "__main__":
75+
import doctest
76+
77+
doctest.testmod()
78+
3679
number = int(input("Enter number: ").strip())
3780
if is_carmichael_number(number):
3881
print(f"{number} is a Carmichael Number.")

0 commit comments

Comments
 (0)