Key Math Formulas for DSA & Programming
Key Math Formulas for DSA & Programming
Fast exponentiation techniques, such as exponentiation by squaring, improve computational efficiency by reducing the number of multiplicative operations required to compute a^b. Instead of performing b multiplications, which is computationally expensive for large b, the technique reduces the problem size by half, performing log b multiplicative steps. This efficient use of operations significantly accelerates computations that involve large exponents, crucial in cryptography and large-scale computations .
Logarithms and exponentials facilitate optimization in algorithms such as binary search by reducing time complexity. Binary search operates in O(log n) time, significantly faster than linear search's O(n) by leveraging log2 function properties to iteratively halve the search space. The concept of logarithmic scaling allows algorithms to efficiently handle large datasets by minimizing operation counts through exponential concepts, leading to profound optimizations in computational tasks and algorithm design .
The Sieve of Eratosthenes is used to find all prime numbers up to a given number n by iteratively marking the multiples of each prime starting from 2. The steps involve creating a list of numbers from 2 to n and continuously marking the multiples of each prime. It is efficient because, once a multiple is marked, it reduces the number of operations required for further prime checks, as opposed to trial division, which checks divisibility for each number separately up to its square root .
Fermat's Little Theorem states that for a prime p, a^(p-1) ≡ 1 (mod p), which implies a^(p-2) ≡ a^(-1) (mod p). It is instrumental in calculating modular inverses, especially when used in combination with the nCr mod m formula in combinatorial problems, because it enables division by a number under a modulo, essentially converting division into multiplication, which is computationally feasible .
The quadratic formula is derived from completing the square of a quadratic equation ax^2 + bx + c = 0. By transforming the equation into the form (x + b/(2a))^2 = (b^2 - 4ac)/(4a^2), the roots are then solved by extracting the square root. The formula x = (-b ± sqrt(b^2 - 4ac)) / (2a) reveals the nature of the roots depending on the discriminant (b^2 - 4ac), allowing one to determine if the roots are real or complex, important for mathematical modeling and problem-solving in various fields .
An adjacency matrix is a 2D array used to represent a graph, where matrix element a[i][j] is non-zero if there is an edge from vertex i to vertex j. It allows quick lookups of edge presence in O(1) time. However, its time and space complexity is O(V^2), where V is the number of vertices, making it less efficient for representing sparse graphs. For large graphs with few edges, an adjacency list, with time complexity O(V + E), is often more space-efficient .
Modular arithmetic simplifies calculations like a^b % m by reducing large numbers to their remainders mod m before computations. The identity (a * b) % m = ((a % m) * (b % m)) % m is used to perform operations with numbers that do not exceed m, avoiding overflow errors. This technique is particularly useful in fast exponentiation methods, enabling large powers to be computed efficiently .
The function __builtin_popcount(), which counts the number of set bits (1s) in an integer, is beneficial in scenarios involving binary data operations, such as cryptography, image processing, or computing parity bits. It is implemented efficiently at the hardware level or through optimized algorithms, allowing rapid counting without manually iterating over each bit. This function is crucial in optimizing time-sensitive applications that require frequent bit manipulations .
Heron's Formula, A = sqrt(s(s-a)(s-b)(s-c)), where s = (a+b+c)/2, is significant because it calculates a triangle's area based solely on the lengths of its sides, without needing the height. This makes it especially useful for irregular or non-right triangles, unlike the traditional base-and-height method, which requires knowing an altitude measurement. Heron's Formula provides a versatile approach to area calculation in computational geometry and design applications .
Catalan Numbers count certain kinds of combinatorial structures, such as valid parentheses sequences or binary tree formations. They are computed using the formula C_n = (1 / (n+1)) * C(2n, n), where C(2n, n) is a binomial coefficient representing combinations. This relationship is derived from counting paths on a grid that do not cross a diagonal, making Catalan numbers a vital tool in combinatorial mathematics for solving structural counting problems .