forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarmichael_number.py
87 lines (64 loc) · 1.49 KB
/
carmichael_number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
== Carmichael Numbers ==
A number n is said to be a Carmichael number if it
satisfies the following modular arithmetic condition:
power(b, n-1) MOD n = 1,
for all b ranging from 1 to n such that b and
n are relatively prime, i.e, gcd(b, n) = 1
Examples of Carmichael Numbers: 561, 1105, ...
https://en.wikipedia.org/wiki/Carmichael_number
"""
def gcd(a: int, b: int) -> int:
if a < b:
return gcd(b, a)
if a % b == 0:
return b
return gcd(b, a % b)
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
temp = (temp * temp) % mod
if y % 2 == 1:
temp = (temp * x) % mod
return temp
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:
return False
b += 1
return True
if __name__ == "__main__":
import doctest
doctest.testmod()