0% found this document useful (0 votes)
41 views3 pages

Key Math Formulas for DSA & Programming

Uploaded by

subhashsuthar818
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views3 pages

Key Math Formulas for DSA & Programming

Uploaded by

subhashsuthar818
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Essential Math Formulas for Programming & DSA

1. Number Theory

- Even/Odd: n % 2 == 0

- Divisibility: a % b == 0

- Prime Check: Up to sqrt(n)

- Sieve of Eratosthenes: Generate all primes n

- GCD: gcd(a, b) = gcd(b, a % b)

- LCM: lcm(a, b) = (a * b) / gcd(a, b)

- Modular Arithmetic:

(a + b) % m = ((a % m) + (b % m)) % m

(a b) % m = ((a % m) (b % m)) % m

a^b % m Fast exponentiation

2. Algebra & Arithmetic

- Sum of 1 to n: n(n+1)/2

- Sum of squares: n(n+1)(2n+1)/6

- Sum of cubes: (n(n+1)/2)^2

- Quadratic Formula: (-b sqrt(b^2 - 4ac)) / 2a

- a^2 - b^2 = (a - b)(a + b)

3. Combinatorics

- Factorial: n!

- Permutations: P(n, r) = n! / (n - r)!

- Combinations: C(n, r) = n! / (r!(n - r)!)

- nCr mod m using Fermat's Little Theorem

4. Geometry
Essential Math Formulas for Programming & DSA

- Distance: sqrt((x2 - x1)^2 + (y2 - y1)^2)

- Triangle area (Heron's): A = sqrt(s(s-a)(s-b)(s-c)), s = (a+b+c)/2

- Circle area: pi * r^2, Perimeter: 2 * pi * r

5. Bit Manipulation

- Check ith bit: (n >> i) & 1

- Set ith bit: n | (1 << i)

- Clear ith bit: n & ~(1 << i)

- Toggle ith bit: n ^ (1 << i)

- Count set bits: __builtin_popcount(n)

- Power of 2: n & (n - 1) == 0

6. Logarithms and Exponentials

- log_b(a) = log_k(a) / log_k(b)

- a^b = e^(b * ln(a))

- Used in Binary Search, Time Complexity

7. Probability (Basics)

- P(E) = Favorable outcomes / Total outcomes

- Useful in randomized algorithms and expected values

8. Graph/Matrix

- Adjacency matrix: O(V^2), List: O(V + E)

- Matrix multiplication: C[i][j] = Sum over k of A[i][k] * B[k][j]


Essential Math Formulas for Programming & DSA

- Floyd-Warshall: dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

9. Miscellaneous

- Catalan Number: C_n = (1 / (n+1)) * C(2n, n)

- Fibonacci: F(n) = F(n-1) + F(n-2)

- Fermat's Little Theorem: a^(p-2) a^(-1) mod p

Common questions

Powered by AI

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 .

You might also like