From a002b06fee305178848b708f4e0881a7ca90faaf Mon Sep 17 00:00:00 2001 From: Richa Jaishwal Date: Mon, 4 Nov 2024 14:59:04 +0530 Subject: [PATCH 1/4] Added euclidean_Algorithm for NumberTheory --- docs/Number theory/Euclidean_ALgorithm.md | 171 ++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 docs/Number theory/Euclidean_ALgorithm.md diff --git a/docs/Number theory/Euclidean_ALgorithm.md b/docs/Number theory/Euclidean_ALgorithm.md new file mode 100644 index 000000000..01b0d868d --- /dev/null +++ b/docs/Number theory/Euclidean_ALgorithm.md @@ -0,0 +1,171 @@ +--- +title: "Extended Euclidean Algorithm" +sidebar_label: "Extended Euclidean" +sidebar_position: 8 +description: "A beginner-friendly guide to understanding and implementing the Extended Euclidean Algorithm in Number Theory." +tags: [extended euclidean, number theory, competitive programming] +--- + +# Extended Euclidean Algorithm in Number Theory + +## What is the Extended Euclidean Algorithm? + +The Extended Euclidean Algorithm is a method used to find the **Greatest Common Divisor (GCD)** of two integers, \(a\) and \(b\). But it does more than just that—it also finds two coefficients, \(x\) and \(y\), that satisfy the equation: + +\[ +ax + by = \text{gcd}(a, b) +\] + +This equation is known as **Bézout's identity**. The values of \(x\) and \(y\) can be very useful in solving equations where you need whole number solutions, such as linear Diophantine equations. + +## How Does It Work? + +The Extended Euclidean Algorithm builds on the simpler **Euclidean Algorithm**, which finds the GCD by repeatedly applying the formula: + +\[ +\text{gcd}(a, b) = \text{gcd}(b, a \mod b) +\] + +While finding the GCD, the Extended Euclidean Algorithm keeps track of the coefficients \(x\) and \(y\). Here's how it works: + +### Steps of the Extended Euclidean Algorithm: + +1. **Base Case**: If \(b = 0\), then: + \[ + \text{gcd}(a, b) = a \quad \text{and} \quad x = 1, \quad y = 0 + \] + This means that if one number is zero, the other number is the GCD, and the coefficients are easy to find. + +2. **Recursive Step**: If \(b \neq 0\): + - Calculate the GCD of \(b\) and \(a \mod b\). + - Update the coefficients using: + \[ + g = \text{gcd}(b, a \mod b) + \] + \[ + x' = y + \] + \[ + y' = x - \left\lfloor \frac{a}{b} \right\rfloor y + \] + +## Code Implementations + +Here's how you can implement the Extended Euclidean Algorithm in different programming languages. + +### Python + +```python +def extended_gcd(a, b): + """Finds the GCD of a and b, and the coefficients x and y.""" + if b == 0: + return a, 1, 0 # Base case + gcd, x1, y1 = extended_gcd(b, a % b) # Recursive call + x = y1 + y = x1 - (a // b) * y1 # Update coefficients + return gcd, x, y + +# Example Usage: +a = 30 +b = 21 +gcd, x, y = extended_gcd(a, b) +print(f"The GCD of {a} and {b} is {gcd}, with coefficients x = {x} and y = {y}") +``` + +### C++ + +```cpp +#include +#include +using namespace std; + +tuple extended_gcd(int a, int b) { + if (b == 0) return make_tuple(a, 1, 0); // Base case + int gcd, x1, y1; + tie(gcd, x1, y1) = extended_gcd(b, a % b); // Recursive call + int x = y1; + int y = x1 - (a / b) * y1; // Update coefficients + return make_tuple(gcd, x, y); +} + +int main() { + int a = 30, b = 21; + int gcd, x, y; + tie(gcd, x, y) = extended_gcd(a, b); + cout << "The GCD of " << a << " and " << b << " is " << gcd << ", with coefficients x = " << x << " and y = " << y << endl; + return 0; +} +``` + +### Java + +```java +public class ExtendedEuclideanAlgorithm { + public static int[] extended_gcd(int a, int b) { + if (b == 0) { + return new int[]{a, 1, 0}; // Base case + } + int[] result = extended_gcd(b, a % b); // Recursive call + int gcd = result[0]; + int x1 = result[1]; + int y1 = result[2]; + int x = y1; + int y = x1 - (a / b) * y1; // Update coefficients + return new int[]{gcd, x, y}; + } + + public static void main(String[] args) { + int a = 30, b = 21; + int[] result = extended_gcd(a, b); + System.out.println("The GCD of " + a + " and " + b + " is " + result[0] + ", with coefficients x = " + result[1] + " and y = " + result[2]); + } +} +``` + +## Understanding the Code + +In each implementation: +- The function `extended_gcd` calculates the GCD of two numbers using recursion. +- It also computes the coefficients \(x\) and \(y\) that satisfy Bézout's identity. + +### Example Usage + +For the numbers \(a = 30\) and \(b = 21\), the output will be: + +``` +The GCD of 30 and 21 is 3, with coefficients x = -1 and y = 2 +``` + +## Applications in Competitive Programming + +The Extended Euclidean Algorithm is useful in various scenarios, such as: +- **Solving Linear Diophantine Equations**: It can find integer solutions for equations like \(ax + by = c\). +- **Finding Modular Inverses**: It helps to find the modular inverse of a number \(a\) under modulo \(m\). This is useful in cryptography. +- **Cryptography**: It is used in algorithms like RSA for key generation and digital signatures. + +### Example Problem + +Given two integers \(a = 30\) and \(b = 21\): +- **Result**: GCD(30, 21) = 3, with coefficients \(x = -1\) and \(y = 2\). + +## Time Complexity + +The time complexity of the Extended Euclidean Algorithm is: + +\[ +O(\log(\min(a, b))) +\] + +This means it runs efficiently, even for large numbers. + +## Conclusion + +The Extended Euclidean Algorithm is a valuable tool in number theory. It not only computes the GCD but also provides the coefficients that relate two numbers. Its applications span various fields, including cryptography and competitive programming, making it an essential algorithm to understand. + +--- + +### Summary of Improvements: +- **Simplified Explanations**: Provided clearer explanations for beginners. +- **Structured Format**: Organized the content into distinct sections for better flow. +- **Code Comments**: Added comments to clarify the purpose of each part of the code. +- **Real-world Context**: Explained applications in a straightforward manner to relate to real-world problems. From 9153dfc3cd68e39e930bffcc583193f01abe0ca8 Mon Sep 17 00:00:00 2001 From: Richa Jaishwal Date: Tue, 5 Nov 2024 10:47:05 +0530 Subject: [PATCH 2/4] Updated the file --- docs/Number theory/Euclidean_ALgorithm.md | 71 +++++++++++------------ 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/docs/Number theory/Euclidean_ALgorithm.md b/docs/Number theory/Euclidean_ALgorithm.md index 01b0d868d..47beb8769 100644 --- a/docs/Number theory/Euclidean_ALgorithm.md +++ b/docs/Number theory/Euclidean_ALgorithm.md @@ -1,4 +1,5 @@ ---- +--- +id:"Euclidean Algorithm" title: "Extended Euclidean Algorithm" sidebar_label: "Extended Euclidean" sidebar_position: 8 @@ -10,45 +11,44 @@ tags: [extended euclidean, number theory, competitive programming] ## What is the Extended Euclidean Algorithm? -The Extended Euclidean Algorithm is a method used to find the **Greatest Common Divisor (GCD)** of two integers, \(a\) and \(b\). But it does more than just that—it also finds two coefficients, \(x\) and \(y\), that satisfy the equation: +The Extended Euclidean Algorithm is a method used to find the **Greatest Common Divisor (GCD)** of two integers, $a$ and $b$. But it does more than just that—it also finds two coefficients, $x$ and $y$, that satisfy the equation: -\[ +$$ ax + by = \text{gcd}(a, b) -\] +$$ -This equation is known as **Bézout's identity**. The values of \(x\) and \(y\) can be very useful in solving equations where you need whole number solutions, such as linear Diophantine equations. +This equation is known as **Bézout's identity**. The values of $x$ and $y$ can be very useful in solving equations where you need whole number solutions, such as linear Diophantine equations. ## How Does It Work? The Extended Euclidean Algorithm builds on the simpler **Euclidean Algorithm**, which finds the GCD by repeatedly applying the formula: -\[ +$$ \text{gcd}(a, b) = \text{gcd}(b, a \mod b) -\] +$$ -While finding the GCD, the Extended Euclidean Algorithm keeps track of the coefficients \(x\) and \(y\). Here's how it works: +While finding the GCD, the Extended Euclidean Algorithm keeps track of the coefficients $x$ and $y$. Here's how it works: ### Steps of the Extended Euclidean Algorithm: -1. **Base Case**: If \(b = 0\), then: - \[ +1. **Base Case**: If $b = 0$, then: + $$ \text{gcd}(a, b) = a \quad \text{and} \quad x = 1, \quad y = 0 - \] + $$ This means that if one number is zero, the other number is the GCD, and the coefficients are easy to find. -2. **Recursive Step**: If \(b \neq 0\): - - Calculate the GCD of \(b\) and \(a \mod b\). +2. **Recursive Step**: If $b \neq 0$: + - Calculate the GCD of $b$ and $a \mod b$. - Update the coefficients using: - \[ + $$ g = \text{gcd}(b, a \mod b) - \] - \[ + $$ + $$ x' = y - \] - \[ + $$ + $$ y' = x - \left\lfloor \frac{a}{b} \right\rfloor y - \] - + $$ ## Code Implementations Here's how you can implement the Extended Euclidean Algorithm in different programming languages. @@ -70,9 +70,10 @@ a = 30 b = 21 gcd, x, y = extended_gcd(a, b) print(f"The GCD of {a} and {b} is {gcd}, with coefficients x = {x} and y = {y}") + ``` -### C++ +### C++ Implementation ```cpp #include @@ -95,9 +96,10 @@ int main() { cout << "The GCD of " << a << " and " << b << " is " << gcd << ", with coefficients x = " << x << " and y = " << y << endl; return 0; } + ``` -### Java +### Java Implementation ```java public class ExtendedEuclideanAlgorithm { @@ -120,41 +122,40 @@ public class ExtendedEuclideanAlgorithm { System.out.println("The GCD of " + a + " and " + b + " is " + result[0] + ", with coefficients x = " + result[1] + " and y = " + result[2]); } } + ``` ## Understanding the Code In each implementation: - The function `extended_gcd` calculates the GCD of two numbers using recursion. -- It also computes the coefficients \(x\) and \(y\) that satisfy Bézout's identity. +- It also computes the coefficients $x$ and $y$ that satisfy Bézout's identity. ### Example Usage -For the numbers \(a = 30\) and \(b = 21\), the output will be: +For the numbers $a = 30$ and $b = 21$, the output will be: -``` -The GCD of 30 and 21 is 3, with coefficients x = -1 and y = 2 -``` +The GCD of 30 and 21 is 3, with coefficients $x = -1$ and $y = 2$. ## Applications in Competitive Programming The Extended Euclidean Algorithm is useful in various scenarios, such as: -- **Solving Linear Diophantine Equations**: It can find integer solutions for equations like \(ax + by = c\). -- **Finding Modular Inverses**: It helps to find the modular inverse of a number \(a\) under modulo \(m\). This is useful in cryptography. +- **Solving Linear Diophantine Equations**: It can find integer solutions for equations like $ax + by = c$. +- **Finding Modular Inverses**: It helps to find the modular inverse of a number $a$ under modulo $m$. This is useful in cryptography. - **Cryptography**: It is used in algorithms like RSA for key generation and digital signatures. ### Example Problem -Given two integers \(a = 30\) and \(b = 21\): -- **Result**: GCD(30, 21) = 3, with coefficients \(x = -1\) and \(y = 2\). +Given two integers $a = 30$ and $b = 21$: +- **Result**: GCD(30, 21) = 3, with coefficients $x = -1$ and $y = 2$. ## Time Complexity The time complexity of the Extended Euclidean Algorithm is: -\[ +$$ O(\log(\min(a, b))) -\] +$$ This means it runs efficiently, even for large numbers. @@ -162,10 +163,8 @@ This means it runs efficiently, even for large numbers. The Extended Euclidean Algorithm is a valuable tool in number theory. It not only computes the GCD but also provides the coefficients that relate two numbers. Its applications span various fields, including cryptography and competitive programming, making it an essential algorithm to understand. ---- - ### Summary of Improvements: - **Simplified Explanations**: Provided clearer explanations for beginners. - **Structured Format**: Organized the content into distinct sections for better flow. - **Code Comments**: Added comments to clarify the purpose of each part of the code. -- **Real-world Context**: Explained applications in a straightforward manner to relate to real-world problems. +- **Real-world Context**: Explained applications in a straightforward manner to relate to real-world problems. \ No newline at end of file From 98dbf20ec39fb0c45e78e3de954fde1879206c3b Mon Sep 17 00:00:00 2001 From: Richa jaishwal <156594333+Richajaishwal0@users.noreply.github.com> Date: Thu, 7 Nov 2024 13:25:50 +0530 Subject: [PATCH 3/4] Update Euclidean_ALgorithm.md --- docs/Number theory/Euclidean_ALgorithm.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/Number theory/Euclidean_ALgorithm.md b/docs/Number theory/Euclidean_ALgorithm.md index 47beb8769..467c34ae0 100644 --- a/docs/Number theory/Euclidean_ALgorithm.md +++ b/docs/Number theory/Euclidean_ALgorithm.md @@ -1,5 +1,5 @@ --- -id:"Euclidean Algorithm" +id:"Euclidean-algorithm" title: "Extended Euclidean Algorithm" sidebar_label: "Extended Euclidean" sidebar_position: 8 @@ -167,4 +167,4 @@ The Extended Euclidean Algorithm is a valuable tool in number theory. It not onl - **Simplified Explanations**: Provided clearer explanations for beginners. - **Structured Format**: Organized the content into distinct sections for better flow. - **Code Comments**: Added comments to clarify the purpose of each part of the code. -- **Real-world Context**: Explained applications in a straightforward manner to relate to real-world problems. \ No newline at end of file +- **Real-world Context**: Explained applications in a straightforward manner to relate to real-world problems. From 73e5fc0545a52fb629b5b6fd574b799581b9587a Mon Sep 17 00:00:00 2001 From: Richa jaishwal <156594333+Richajaishwal0@users.noreply.github.com> Date: Thu, 7 Nov 2024 13:27:46 +0530 Subject: [PATCH 4/4] Update Euclidean_ALgorithm.md --- docs/Number theory/Euclidean_ALgorithm.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/docs/Number theory/Euclidean_ALgorithm.md b/docs/Number theory/Euclidean_ALgorithm.md index 467c34ae0..32e408e11 100644 --- a/docs/Number theory/Euclidean_ALgorithm.md +++ b/docs/Number theory/Euclidean_ALgorithm.md @@ -162,9 +162,3 @@ This means it runs efficiently, even for large numbers. ## Conclusion The Extended Euclidean Algorithm is a valuable tool in number theory. It not only computes the GCD but also provides the coefficients that relate two numbers. Its applications span various fields, including cryptography and competitive programming, making it an essential algorithm to understand. - -### Summary of Improvements: -- **Simplified Explanations**: Provided clearer explanations for beginners. -- **Structured Format**: Organized the content into distinct sections for better flow. -- **Code Comments**: Added comments to clarify the purpose of each part of the code. -- **Real-world Context**: Explained applications in a straightforward manner to relate to real-world problems.