mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-02 17:31:08 +00:00
583a614fef
* Deleted greatest_common_divisor def from many files and instead imported the method from Maths folder * Deleted greatest_common_divisor def from many files and instead imported the method from Maths folder, also fixed comments * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Deleted greatest_common_divisor def from many files and instead imported the method from Maths folder, also fixed comments * Imports organized * recursive gcd function implementation rolledback * more gcd duplicates removed * more gcd duplicates removed * Update maths/carmichael_number.py * updated files * moved a file to another location --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
"""
|
|
== 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
|
|
"""
|
|
from maths.greatest_common_divisor import greatest_common_divisor
|
|
|
|
|
|
def power(x: int, y: int, mod: int) -> int:
|
|
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:
|
|
b = 2
|
|
while b < n:
|
|
if greatest_common_divisor(b, n) == 1 and power(b, n - 1, n) != 1:
|
|
return False
|
|
b += 1
|
|
return True
|
|
|
|
|
|
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.")
|