Removed redundant greatest_common_divisor code (#9358)
* 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>
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from maths.greatest_common_divisor import greatest_common_divisor
|
||||
|
||||
|
||||
def diophantine(a: int, b: int, c: int) -> tuple[float, float]:
|
||||
"""
|
||||
Diophantine Equation : Given integers a,b,c ( at least one of a and b != 0), the
|
||||
diophantine equation a*x + b*y = c has a solution (where x and y are integers)
|
||||
iff gcd(a,b) divides c.
|
||||
iff greatest_common_divisor(a,b) divides c.
|
||||
|
||||
GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
|
||||
|
||||
@@ -22,7 +24,7 @@ def diophantine(a: int, b: int, c: int) -> tuple[float, float]:
|
||||
|
||||
assert (
|
||||
c % greatest_common_divisor(a, b) == 0
|
||||
) # greatest_common_divisor(a,b) function implemented below
|
||||
) # greatest_common_divisor(a,b) is in maths directory
|
||||
(d, x, y) = extended_gcd(a, b) # extended_gcd(a,b) function implemented below
|
||||
r = c / d
|
||||
return (r * x, r * y)
|
||||
@@ -69,32 +71,6 @@ def diophantine_all_soln(a: int, b: int, c: int, n: int = 2) -> None:
|
||||
print(x, y)
|
||||
|
||||
|
||||
def greatest_common_divisor(a: int, b: int) -> int:
|
||||
"""
|
||||
Euclid's Lemma : d divides a and b, if and only if d divides a-b and b
|
||||
|
||||
Euclid's Algorithm
|
||||
|
||||
>>> greatest_common_divisor(7,5)
|
||||
1
|
||||
|
||||
Note : In number theory, two integers a and b are said to be relatively prime,
|
||||
mutually prime, or co-prime if the only positive integer (factor) that
|
||||
divides both of them is 1 i.e., gcd(a,b) = 1.
|
||||
|
||||
>>> greatest_common_divisor(121, 11)
|
||||
11
|
||||
|
||||
"""
|
||||
if a < b:
|
||||
a, b = b, a
|
||||
|
||||
while a % b != 0:
|
||||
a, b = b, a % b
|
||||
|
||||
return b
|
||||
|
||||
|
||||
def extended_gcd(a: int, b: int) -> tuple[int, int, int]:
|
||||
"""
|
||||
Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers
|
||||
|
||||
Reference in New Issue
Block a user