From 5505914c258f6eefafbe4440ca9f610c14e155bf Mon Sep 17 00:00:00 2001 From: Anushka Chouhan Date: Thu, 7 Nov 2024 20:18:42 +0530 Subject: [PATCH 1/3] Update Number Theory --- .../Divisibility and Prime Numbers.md | 76 +++++++++ docs/Number theory/Modular Arithmetic.md | 150 ++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 docs/Number theory/Divisibility and Prime Numbers.md create mode 100644 docs/Number theory/Modular Arithmetic.md diff --git a/docs/Number theory/Divisibility and Prime Numbers.md b/docs/Number theory/Divisibility and Prime Numbers.md new file mode 100644 index 000000000..3bb0be02d --- /dev/null +++ b/docs/Number theory/Divisibility and Prime Numbers.md @@ -0,0 +1,76 @@ +--- +id: Divisibility-and-Prime-Numbers +title: "Divisibility and Prime Numbers" +sidebar_label: "Divisibility and Prime Numbers" +sidebar_position: 10 +description: "A detailed guide to understanding and implementing the Divisibility and Prime Numbers." +tags: [Divisibility, Prime Numbers, number theory, competitive programming] +--- + +# Divisibility and Prime Numbers + +Understanding divisibility and prime numbers is essential in number theory, with applications in cryptography, algorithm optimization, and more. + +## Divisibility + +Divisibility rules help quickly determine whether a number is divisible by another without performing full division. Here are some basic divisibility rules: + +1. **Divisibility by 2**: A number is divisible by 2 if its last digit is even. +2. **Divisibility by 3**: A number is divisible by 3 if the sum of its digits is divisible by 3. + ```python + def is_divisible_by_3(n): + return sum(map(int, str(n))) % 3 == 0 + ``` +3. **Divisibility by 5**: A number is divisible by 5 if its last digit is 0 or 5. + ```python + def is_divisible_by_5(n): + return str(n)[-1] in ('0', '5') + ``` +4. **Divisibility by 9**: A number is divisible by 9 if the sum of its digits is divisible by 9. + ```python + def is_divisible_by_9(n): + return sum(map(int, str(n))) % 9 == 0 + ``` +5. **Divisibility by 10**: A number is divisible by 10 if it ends in 0. + +### Examples + +- **Is 123 divisible by 3?** Sum of digits: 1 + 2 + 3 = 6, which is divisible by 3, so 123 is also divisible by 3. +- **Is 145 divisible by 5?** Last digit is 5, so it is divisible by 5. + +## Prime Numbers + +A **prime number** is a natural number greater than 1 that has no divisors other than 1 and itself. + +### Properties of Prime Numbers + +1. **Uniqueness**: Each natural number greater than 1 is either a prime itself or can be factored uniquely into prime numbers (Fundamental Theorem of Arithmetic). +2. **Infinitude**: There are infinitely many prime numbers. + +### Prime Checking Algorithms + +1. **Trial Division**: Check if the number is divisible by any number up to its square root. +2. **Sieve of Eratosthenes**: An efficient way to find all primes up to a certain limit. + +### Prime-Related Concepts + +- **Co-primes**: Two numbers are co-prime if their greatest common divisor (GCD) is 1. +- **Prime Factorization**: Breaking down a number into the product of prime numbers. For example, 60 = 2 * 2 * 3 * 5. + +### Prime Number Theorems + +1. **Prime Density**: The density of prime numbers decreases as numbers get larger. +2. **Approximation**: The Prime Number Theorem approximates the number of primes less than a given number \( n \) as \( n / \ln(n) \). + +## Practice Problems + +- [Check If Number Is Prime](https://leetcode.com/problems/check-if-number-is-prime/) +- [Prime Factorization](https://www.geeksforgeeks.org/prime-factorization/) +- [Sieve of Eratosthenes Implementation](https://leetcode.com/problems/count-primes/) +- [Find GCD of Array (Co-primes)](https://leetcode.com/problems/find-greatest-common-divisor-of-array/) + +--- + +By understanding divisibility rules and the properties of prime numbers, you can solve complex problems related to number theory more efficiently. + + diff --git a/docs/Number theory/Modular Arithmetic.md b/docs/Number theory/Modular Arithmetic.md new file mode 100644 index 000000000..67a0a0fb6 --- /dev/null +++ b/docs/Number theory/Modular Arithmetic.md @@ -0,0 +1,150 @@ +--- +id: Modular Arithmetic +title: "Modular Arithmetic" +sidebar_label: "Number theory" +sidebar_position: 10 +description: "A detailed guide to understanding and implementing the Modular Arithmetic in Number Theory." +tags: [Modular Arithmetic, number theory, competitive programming] +--- + +# Modular Arithmetic + +Modular arithmetic is a system of arithmetic for integers, where numbers wrap around after reaching a certain value—the modulus. It is commonly used in computer science and number theory. Below, we will cover the basics, properties, applications, and examples of modular arithmetic. + +## Basics of Modular Arithmetic + +In modular arithmetic, we work with the remainder when one integer is divided by another. We write: + +``` +a ≡ b (mod m) +``` +to mean that **a** and **b** leave the same remainder when divided by **m**. This is equivalent to saying that **(a - b)** is divisible by **m**. + +### Example + +For example, if **a = 17**, **b = 5**, and **m = 12**, we find: + +``` +17 ≡ 5 (mod 12) +``` +since 17 and 5 both leave a remainder of 5 when divided by 12. + +## Properties of Modular Arithmetic + +Modular arithmetic has several useful properties: + +1. **Addition**: + - `(a + b) % m = ((a % m) + (b % m)) % m` + +2. **Subtraction**: + - `(a - b) % m = ((a % m) - (b % m) + m) % m` + +3. **Multiplication**: + - `(a * b) % m = ((a % m) * (b % m)) % m` + +4. **Exponentiation**: + - `(a^b) % m = ((a % m)^b) % m` + +5. **Division (Inverse Modulo)**: + - Division is not directly supported, but you can divide by multiplying with the modular inverse of the divisor. + +## Applications of Modular Arithmetic + +Modular arithmetic is widely used in many fields, including: + +1. **Cryptography**: Algorithms like RSA use modular arithmetic to create secure encryption schemes. + +2. **Hashing Algorithms**: Modular arithmetic helps ensure that hash values are within a specific range. + +3. **Clock Arithmetic**: Modular arithmetic is used in clocks, as hours cycle back to 0 after reaching 12 or 24. + +4. **Random Number Generation**: It is also used to keep numbers within a certain range, which is crucial for generating random numbers. + +5. **Computer Graphics**: Modular arithmetic can help with cyclic transformations in graphics programming. + +## Modular Exponentiation + +Modular exponentiation is the process of finding `(base^exponent) % modulus` efficiently. This is especially useful in cryptography. + +### Fast Exponentiation Algorithm + +1. **Recursive Method**: + ```python + def mod_exp(base, exponent, modulus): + if exponent == 0: + return 1 + half_exp = mod_exp(base, exponent // 2, modulus) + half_exp = (half_exp * half_exp) % modulus + if exponent % 2 != 0: + half_exp = (half_exp * base) % modulus + return half_exp + + ``` + +2. **Recursive Method**: + ```python + def mod_exp(base, exponent, modulus): + result = 1 + base = base % modulus + while exponent > 0: + if (exponent % 2) == 1: + result = (result * base) % modulus + exponent = exponent >> 1 + base = (base * base) % modulus + return result + ``` +## Modular Inverse +To divide by a number under a modulus, we use the modular inverse. The modular inverse of a modulo m is a number x such that: +``` +a * x ≡ 1 (mod m) +``` +The modular inverse exists if and only if a and m are coprime. + +## Finding Modular Inverse +1. **Using Extended Euclidean Algorithm:** + +```python + def extended_gcd(a, b): + if a == 0: + return b, 0, 1 + gcd, x1, y1 = extended_gcd(b % a, a) + x = y1 - (b // a) * x1 + y = x1 + return gcd, x, y + +def mod_inverse(a, m): + gcd, x, _ = extended_gcd(a, m) + if gcd != 1: + return None # Inverse does not exist + return x % m +``` +2. **Using Fermat’s Little Theorem (when m is prime):** + + If `m` is a prime number, the modular inverse of `a` can be calculated as: + +``` +a^(m-2) % m +``` +## Practice Problems +Here are some practice problems to solidify your understanding of modular arithmetic: + +- [Chinese Remainder Theorem](https://leetcode.com/problems/chinese-remainder-theorem/) +- [Modulo Exponentiation](https://leetcode.com/problems/powx-n/) +- [Modular Multiplicative Inverse](https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/) + +### Further Practice Resources + +For more extensive problem sets and further explanations, consider exploring: + +- [Modular Arithmetic Exercises on HackerRank](https://www.hackerrank.com/domains/tutorials/10-days-of-math) +- [Advanced Modular Arithmetic Problems on CodeSignal](https://codesignal.com/interview-practice/) + +By solving these problems, you can deepen your understanding and mastery of modular arithmetic in various applications. + +## Key Points to Remember +- **Wrapping Around**: Numbers reset to 0 after reaching the modulus. +- **Commutative and Associative Properties**: Useful for rearranging calculations in modular arithmetic. +- **Negative Results**: For a result `(a - b) % m`, add `m` if the result is negative to keep within the correct range. + +--- + From be60fdbfac784f83403784ed1f8284699073c9dc Mon Sep 17 00:00:00 2001 From: Anushka Chouhan <157525924+AnushkaChouhan25@users.noreply.github.com> Date: Sat, 9 Nov 2024 14:05:57 +0530 Subject: [PATCH 2/3] Update Divisibility and Prime Numbers.md --- docs/Number theory/Divisibility and Prime Numbers.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/Number theory/Divisibility and Prime Numbers.md b/docs/Number theory/Divisibility and Prime Numbers.md index 3bb0be02d..6a581d0b2 100644 --- a/docs/Number theory/Divisibility and Prime Numbers.md +++ b/docs/Number theory/Divisibility and Prime Numbers.md @@ -1,5 +1,5 @@ --- -id: Divisibility-and-Prime-Numbers +id: divisibility-and-prime-numbers title: "Divisibility and Prime Numbers" sidebar_label: "Divisibility and Prime Numbers" sidebar_position: 10 @@ -60,7 +60,7 @@ A **prime number** is a natural number greater than 1 that has no divisors other ### Prime Number Theorems 1. **Prime Density**: The density of prime numbers decreases as numbers get larger. -2. **Approximation**: The Prime Number Theorem approximates the number of primes less than a given number \( n \) as \( n / \ln(n) \). +2. **Approximation**: The Prime Number Theorem approximates the number of primes less than a given number $n$ as $n \ln(n) $. ## Practice Problems From 159fc41c7411bcd01718aefb650dd65204a3d09b Mon Sep 17 00:00:00 2001 From: Anushka Chouhan <157525924+AnushkaChouhan25@users.noreply.github.com> Date: Sat, 9 Nov 2024 14:06:29 +0530 Subject: [PATCH 3/3] Update Modular Arithmetic.md --- docs/Number theory/Modular Arithmetic.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Number theory/Modular Arithmetic.md b/docs/Number theory/Modular Arithmetic.md index 67a0a0fb6..2b91ad03b 100644 --- a/docs/Number theory/Modular Arithmetic.md +++ b/docs/Number theory/Modular Arithmetic.md @@ -1,5 +1,5 @@ --- -id: Modular Arithmetic +id: modular-arithmetic title: "Modular Arithmetic" sidebar_label: "Number theory" sidebar_position: 10