From ef152c8895bc77f54fdc96e3ac1d75d9a5258677 Mon Sep 17 00:00:00 2001 From: Shubhajit Roy <81477286+shubhajitroy123@users.noreply.github.com> Date: Sun, 9 Oct 2022 12:12:00 +0530 Subject: [PATCH 1/9] Add files via upload Python program to determine whether a number is Carmichael Number or not. --- maths/Carmichael Number.py | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 maths/Carmichael Number.py diff --git a/maths/Carmichael Number.py b/maths/Carmichael Number.py new file mode 100644 index 000000000000..6af78c70ce7d --- /dev/null +++ b/maths/Carmichael Number.py @@ -0,0 +1,43 @@ +""" +== 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, b) : + if (a < b) : + return gcd(b, a) + if (a % b == 0) : + return b + return gcd(b, a % b) + +def power(x, y, mod) : + 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) : + b = 2 + while b Date: Sun, 9 Oct 2022 12:20:46 +0530 Subject: [PATCH 2/9] Rename Carmichael Number.py to carmichael number.py --- maths/{Carmichael Number.py => carmichael number.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename maths/{Carmichael Number.py => carmichael number.py} (100%) diff --git a/maths/Carmichael Number.py b/maths/carmichael number.py similarity index 100% rename from maths/Carmichael Number.py rename to maths/carmichael number.py From 26effa44e44037a2f788193da911d0bb4a1cf2c9 Mon Sep 17 00:00:00 2001 From: Shubhajit Roy <81477286+shubhajitroy123@users.noreply.github.com> Date: Sun, 9 Oct 2022 12:21:17 +0530 Subject: [PATCH 3/9] Rename carmichael number.py to carmichael_number.py --- maths/{carmichael number.py => carmichael_number.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename maths/{carmichael number.py => carmichael_number.py} (100%) diff --git a/maths/carmichael number.py b/maths/carmichael_number.py similarity index 100% rename from maths/carmichael number.py rename to maths/carmichael_number.py From 35ced9460a789876c1497d2c485e6a1a78008934 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 9 Oct 2022 06:54:25 +0000 Subject: [PATCH 4/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/carmichael_number.py | 68 +++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 6af78c70ce7d..3c4360b52758 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -1,43 +1,49 @@ """ == Carmichael Numbers == -A number n is said to be a Carmichael number if it satisfies the following modular arithmetic condition: +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 + 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, b) : - if (a < b) : - return gcd(b, a) - if (a % b == 0) : - return b - return gcd(b, a % b) - -def power(x, y, mod) : - 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) : - b = 2 - while b Date: Sun, 9 Oct 2022 15:12:50 +0530 Subject: [PATCH 5/9] Update carmichael_number.py --- maths/carmichael_number.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 3c4360b52758..54b935b295ca 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -1,6 +1,7 @@ """ == Carmichael Numbers == -A number n is said to be a Carmichael number if it satisfies the following modular arithmetic condition: +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 @@ -11,7 +12,7 @@ """ -def gcd(a, b): +def gcd(a: int, b: int): if a < b: return gcd(b, a) if a % b == 0: @@ -42,8 +43,8 @@ def isCarmichaelNumber(n): if __name__ == "__main__": - print("Program to check whether a number is a Carmichael Numbers or not...") number = int(input("Enter number: ").strip()) - print( - f"{number} is {'' if isCarmichaelNumber(number) else 'not '}a Carmichael Number." - ) + if isCarmichaelNumber(number): + print(f"{number} is a Carmichael Number.") + else + print(f"{number} is not a Carmichael Number.") From 1531bf300d105679d1aabbcf27373af5800e5b1f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 9 Oct 2022 09:43:59 +0000 Subject: [PATCH 6/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/carmichael_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 54b935b295ca..784247cf00d6 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -1,6 +1,6 @@ """ == Carmichael Numbers == -A number n is said to be a Carmichael number if it +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, From d3ab1562ad58422ce74caac818ed371d10f522e9 Mon Sep 17 00:00:00 2001 From: Shubhajit Roy <81477286+shubhajitroy123@users.noreply.github.com> Date: Sun, 9 Oct 2022 15:16:58 +0530 Subject: [PATCH 7/9] Create carmichael_number.py --- maths/carmichael_number.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 784247cf00d6..39f54a1b79c9 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -1,6 +1,6 @@ """ == Carmichael Numbers == -A number n is said to be a Carmichael number if it +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, @@ -12,7 +12,7 @@ """ -def gcd(a: int, b: int): +def gcd(a: int, b: int) -> int: if a < b: return gcd(b, a) if a % b == 0: @@ -20,7 +20,7 @@ def gcd(a: int, b: int): return gcd(b, a % b) -def power(x, y, mod): +def power(x: int, y: int, mod: int) -> int: if y == 0: return 1 temp = power(x, y // 2, mod) % mod @@ -30,7 +30,7 @@ def power(x, y, mod): return temp -def isCarmichaelNumber(n): +def isCarmichaelNumber(n: int) -> bool: b = 2 while b < n: @@ -46,5 +46,5 @@ def isCarmichaelNumber(n): number = int(input("Enter number: ").strip()) if isCarmichaelNumber(number): print(f"{number} is a Carmichael Number.") - else + else: print(f"{number} is not a Carmichael Number.") From 0a39468604f1f8fb1217a6e10b4db5754620cffe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 9 Oct 2022 09:47:50 +0000 Subject: [PATCH 8/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/carmichael_number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index 39f54a1b79c9..e8e2f37b3f78 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -1,6 +1,6 @@ """ == Carmichael Numbers == -A number n is said to be a Carmichael number if it +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, From 2dd3e39bf87895175f21554975dde87887ed1bad Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 12 Oct 2022 09:21:28 +0200 Subject: [PATCH 9/9] Update maths/carmichael_number.py --- maths/carmichael_number.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/maths/carmichael_number.py b/maths/carmichael_number.py index e8e2f37b3f78..09a4fedfb763 100644 --- a/maths/carmichael_number.py +++ b/maths/carmichael_number.py @@ -33,12 +33,9 @@ def power(x: int, y: int, mod: int) -> int: def isCarmichaelNumber(n: int) -> bool: b = 2 while b < n: - - if gcd(b, n) == 1: - - if power(b, n - 1, n) != 1: - return False - b = b + 1 + if gcd(b, n) == 1 and power(b, n - 1, n) != 1: + return False + b += 1 return True