mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
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 ad217c6616
.
* 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 <tianyizheng02@gmail.com>
* 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 <tianyizheng02@gmail.com>
This commit is contained in:
parent
5be5d21bed
commit
9a5a6c663c
|
@ -10,10 +10,21 @@ satisfies the following modular arithmetic condition:
|
||||||
Examples of Carmichael Numbers: 561, 1105, ...
|
Examples of Carmichael Numbers: 561, 1105, ...
|
||||||
https://en.wikipedia.org/wiki/Carmichael_number
|
https://en.wikipedia.org/wiki/Carmichael_number
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from maths.greatest_common_divisor import greatest_common_divisor
|
from maths.greatest_common_divisor import greatest_common_divisor
|
||||||
|
|
||||||
|
|
||||||
def power(x: int, y: int, mod: 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:
|
if y == 0:
|
||||||
return 1
|
return 1
|
||||||
temp = power(x, y // 2, mod) % mod
|
temp = power(x, y // 2, mod) % mod
|
||||||
|
@ -24,15 +35,47 @@ def power(x: int, y: int, mod: int) -> int:
|
||||||
|
|
||||||
|
|
||||||
def is_carmichael_number(n: int) -> bool:
|
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:
|
Examples:
|
||||||
return False
|
>>> is_carmichael_number(562)
|
||||||
b += 1
|
False
|
||||||
return True
|
|
||||||
|
>>> is_carmichael_number(561)
|
||||||
|
True
|
||||||
|
|
||||||
|
>>> is_carmichael_number(5.1)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Number 5.1 must instead be a positive integer
|
||||||
|
|
||||||
|
>>> is_carmichael_number(-7)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Number -7 must instead be a positive integer
|
||||||
|
|
||||||
|
>>> is_carmichael_number(0)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Number 0 must instead be a positive integer
|
||||||
|
"""
|
||||||
|
|
||||||
|
if n <= 0 or not isinstance(n, int):
|
||||||
|
msg = f"Number {n} must instead be a positive integer"
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
return all(
|
||||||
|
power(b, n - 1, n) == 1
|
||||||
|
for b in range(2, n)
|
||||||
|
if greatest_common_divisor(b, n) == 1
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
|
||||||
number = int(input("Enter number: ").strip())
|
number = int(input("Enter number: ").strip())
|
||||||
if is_carmichael_number(number):
|
if is_carmichael_number(number):
|
||||||
print(f"{number} is a Carmichael Number.")
|
print(f"{number} is a Carmichael Number.")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user