Skip to content

Python program for Carmicheal Number #6864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 12, 2022
50 changes: 50 additions & 0 deletions maths/carmichael_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
== 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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file maths/carmichael_number.py, please provide doctest for the function gcd

Please provide descriptive name for the parameter: a

Please provide descriptive name for the parameter: b

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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file maths/carmichael_number.py, please provide doctest for the function power

Please provide descriptive name for the parameter: x

Please provide descriptive name for the parameter: y

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 isCarmichaelNumber(n: int) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: isCarmichaelNumber

As there is no test file in this pull request nor any test function or class in the file maths/carmichael_number.py, please provide doctest for the function isCarmichaelNumber

Please provide descriptive name for the parameter: n

b = 2
while b < n:

if gcd(b, n) == 1:

if power(b, n - 1, n) != 1:
return False
b = b + 1
return True


if __name__ == "__main__":
number = int(input("Enter number: ").strip())
if isCarmichaelNumber(number):
print(f"{number} is a Carmichael Number.")
else:
print(f"{number} is not a Carmichael Number.")