w3resource

DSA - Math Essentials for Programming

Basic Mathematics:

Basic mathematics refers to a foundational understanding and proficiency in fundamental mathematical concepts. In the context of programming and problem-solving, comfort with basic mathematics is crucial for performing arithmetic operations, handling numerical data, and implementing algorithms that involve mathematical calculations.

Key components:

  • Arithmetic Operations:
    • Basic mathematical operations include addition, subtraction, multiplication, and division.
    • Example:
    • 5 + 4 = 9 
      12 - 5 = 7 
      3 * 6 = 18 
      10 / 2 = 5 
      
  • Logarithms:
    • Logarithms represent the exponent to which a base must be raised to obtain a given number.
    • Example:
    • log2(8) = 3 // 23 = 8

      log10(100) = 2 // 102 = 100

  • Exponents:
    • Exponents indicate the number of times a value is multiplied by itself.
    • Example:
    • 23 = 2 * 2 * 2 = 8

      52 = 5 * 5 = 25

  • Basic Algebraic Concepts:
    • Understanding variables, equations, and basic algebraic manipulations.
    • Example:
    • 2x + 3 = 7 // Solve for x: x = 2

Applications in programming:

  • Variable manipulation:
    • Example:
    • x = 5
      y = x * 2

  • Numerical Computations:
    • Example:
    • result = (10 + 7) * 3 / 2

  • Algorithmic Calculations:
    • Example (Python code):
    • 
      # Euclidean Algorithm for GCD
      def gcd(x, y):
      while y:
      x, y = y, x % y
      return x

Understanding:

  • Arithmetic Operations: Fundamental operations like addition, subtraction, multiplication, and division.
  • Logarithms: Understanding logarithms and their applications.
  • Exponents: Taking into consideration the concept of exponents and their role in mathematical expressions.
  • Basic Algebraic Concepts: Manipulating variables, solving equations, and basic algebraic expressions.

Number theory:

Number theory is a branch of mathematics that deals with numbers' properties and relationships. In programming and computer science, a basic understanding of number theory concepts is valuable for designing algorithms, cryptography, and optimizing certain computations.

Key Concepts:

  • Prime numbers:
    • Prime numbers are natural numbers greater than 1 that have no positive divisors other than 1 and themselves.
    • Example:
    • 2, 3, 5, 7, 11, 13, 17, ...

  • Divisibility:
    • A number is divisible by another if the division results in a quotient without a remainder.
    • Example:
    • 18 is divisible by 3 because 18 / 3 = 6

  • Modular Arithmetic:
    • Modular arithmetic involves arithmetic operations performed on remainders when integers are divided by a fixed modulus.
    • Example:
    • (17 mod 4) = 1 // The remainder when 17 is divided by 4

  • Greatest Common Divisor (GCD):
    • The GCD of two numbers is the largest positive integer that divides both numbers.
    • Example:
    • GCD(24, 36) = 12

  • Least Common Multiple (LCM):
    • The LCM of two numbers is the smallest positive integer that is a multiple of both numbers.
    • Example:
    • LCM(12, 15) = 60

Applications in Programming:

  • Prime Factorization:
    • Example (Python code):
    • def prime_factorization(n):
          factors = []
          divisor = 2
          while n > 1:
              while n % divisor == 0:
                  factors.append(divisor)
                  n //= divisor
              divisor += 1
          return factors
      	
  • Modular Exponentiation:
    • Example (Python code):
    • def mod_exp(base, exponent, modulus):
          result = 1
          base = base % modulus
          while exponent > 0:
              if exponent % 2 == 1:
                  result = (result * base) % modulus
              exponent //= 2
              base = (base * base) % modulus
          return result
      	
  • Euclidean Algorithm (GCD):
    • Example (Python code):
    • def gcd(a, b):
          while b:
              a, b = b, a % b
          return a 
      	

Understanding:

  • Prime Numbers: Numbers with no divisors except 1 and themselves.
  • Divisibility: Numbers that can be evenly divided without a remainder.
  • Modular Arithmetic: Arithmetic operations performed on remainders with a fixed modulus.
  • GCD and LCM: Greatest Common Divisor and Least Common Multiple.


Follow us on Facebook and Twitter for latest update.