blog post
Essential math concepts for self-taught Software Engineers
By Luis Velazquez Bilbao • May 1, 2022
Contents
Table of Contents
Introduction

As a self-taught software engineer, it can be challenging to figure out which math concepts are essential for success in the field. While there's no one-size-fits-all answer, there are a few fundamental math concepts that every programmer should study.

Each math concept will be illustrated with a JavaScript code example.

Algebra is the foundation of all mathematics and is used in many programming tasks. Understanding algebraic concepts such as variables, equations, and functions is crucial for designing algorithms and solving complex problems.

    
javascript
Copy
// Solving for x in a linear equation: y = mx + b const m = 2; const b = 5; const y = 11; const x = (y - b) / m; console.log('x = ' + x); // Output: x = 3 // Evaluating a polynomial expression: f(x) = x^2 + 2x + 1 const f = (x) => x**2 + 2*x + 1; console.log('f(3) = ' + f(3)); // Output: f(3) = 16

The above JavaScript code demonstrates two mathematical operations in programming:

1. Solving for x in a linear equation:

  • The equation is y = mx + b , where m is the slope , b is the y-intercept , and x and y are variables.
  • The values of m , b , and y are assigned as 2 , 5 , and 11 respectively.
  • To find the value of x , the formula (y - b) / m is used.
  • The result is stored in the variable x .
  • The line console.log(`x = ${x}`); outputs the value of x , which will be 3 in this case.

2. Evaluating a polynomial expression:

  • The expression is f(x) = x2 + 2x + 1 , where f is the function and x is the variable.
  • A function is defined with the name f that takes a parameter x .
  • Inside the function, the polynomial expression is evaluated using the exponentiation operator ** and addition operator + .
  • The line console.log(`f(3) = ${f(3)}`); calculates the value of the function f at x = 3 and outputs the result, which will be 16 in this case.

Calculus is an important area of mathematics that deals with rates of change and accumulation. It is used extensively in machine learning, data science, and optimization problems. Understanding concepts such as limits, derivatives, and integrals can help you build complex systems that perform well.

    
javascript
Copy
// Finding the derivative of a function: f(x) = x^2 const f = (x) => x**2; const df = (x) => 2*x; console.log('f(3) = ' + df(3)); // Output: f(3) = 6 // Integrating a function: f(x) = x^2 const f = (x) => x**2; const a = 0; const b = 3; const dx = 0.001; // Step size let sum = 0; for (let x = a; x <= b; x += dx) { sum += f(x) * dx; } console.log('∫[0,3] f(x) dx = ' + sum); // Output: ∫[0,3] f(x) dx = 9.0004995'

The above JavaScript code demonstrates two mathematical operations in programming:

1. Finding the derivative of a function:

  • The function f represents the original function f(x) = x2 .
  • The function df represents the derivative of f(x) , which is calculated as 2*x .
  • The line console.log(`f(3) = ${df(3)}`); calculates the derivative of f at x = 3 and outputs the result, which will be 6 in this case.

2. Integrating a function:

  • The function f represents the original function f(x) = x2 .
  • The variables a and b represent the limits of integration, where the integral is evaluated over the interval [a, b] .
  • The variable dx represents the step size or the width of each small interval used in the numerical integration process.
  • The code uses a for loop to iterate from a to b with increments of dx .
  • Within each iteration, the value of f(x) is multiplied by dx and added to the sum variable.
  • The line console.log(`∫[0,3] f(x) dx = ${sum}`); calculates the definite integral of f(x) from 0 to 3 using numerical integration and outputs the result, which will be approximately 9.0004995 in this case (subject to the chosen step size, dx ).

Probability and statistics are essential for data analysis and machine learning. Understanding concepts such as mean, median, standard deviation, and correlation can help you analyze data and make informed decisions. Probability theory is also important for designing algorithms that make decisions based on uncertain or random events.

    
javascript
Copy
// Calculating the mean and standard deviation of an array of numbers const data = [1, 3, 5, 7, 9]; const mean = data.reduce((acc, x) => acc + x) / data.length; const sd = Math.sqrt(data.reduce((acc, x) => acc + (x - mean)**2) / (data.length - 1)); console.log('Mean: ' + mean + ', Standard Deviation: ' + sd); // Output: Mean: 5, Standard Deviation: 3.1622776601683795 // Generating a random number between 0 and 1 const rand = Math.random(); console.log('Random number between 0 and 1: ' + rand); // Output: Random number between 0 and 1: 0.8422360437154129'

The above JavaScript code demonstrates two different operations:

1. Calculating the mean and standard deviation of an array of numbers:

  • The array data contains the numbers [1, 3, 5, 7, 9] .
  • The mean is calculated by using the reduce function to sum up all the numbers in the array (data.reduce((acc, x) => acc + x)) and dividing the sum by the length of the array (data.length) .
  • The standard deviation is calculated in two steps:
    • First, the sum of the squared differences between each number and the mean is calculated using the reduce function (data.reduce((acc, x) => acc + (x - mean)**2)) .
    • Then, this sum is divided by (data.length - 1) and the square root of the result is taken using Math.sqrt to obtain the standard deviation.
  • The line console.log(`Mean: ${mean}, Standard Deviation: ${sd}`); outputs the calculated mean and standard deviation, which will be Mean: 5 , Standard Deviation: 3.1622776601683795 in this case.

2. Generating a random number between 0 and 1:

  • The Math.random() function generates a random decimal number between 0 (inclusive) and 1 (exclusive).
  • The line console.log(`Random number between 0 and 1: ${rand}`); outputs the generated random number, which will be a different value each time it's executed, such as Random number between 0 and 1: 0.8422360437154129.

Discrete mathematics deals with discrete objects and can be used in designing algorithms and data structures. Understanding concepts such as sets, graphs, and combinatorics can help you create efficient algorithms and solve complex problems.

    
javascript
Copy
// Checking if an array contains a specific value using sets const data = [1, 3, 5, 7, 9]; const set = new Set(data); const value = 7; console.log('Array contains ' + value + ': ' + set.has(value)); // Output: Array contains 7: true // Traversing a graph using depth-first search const graph = { A: ['B', 'C'], B: ['D', 'E'], C: ['F', 'G'], D: [], E: [], F: [], G: [] }; const dfs = (node) => { console.log(node); for (const neighbor of graph[node]) { dfs(neighbor); } }; dfs('A'); // Output: A B D E C F G'

The above JavaScript code demonstrates two different operations:

1. Checking if an array contains a specific value using sets:

  • The array data contains the numbers [1, 3, 5, 7, 9] .
  • A set is created using new Set(data) , which converts the array into a set. Sets are data structures that allow storing unique values.
  • The variable value is assigned the value 7 .
  • The line console.log(`Array contains ${value}: ${set.has(value)}`); checks if the set contains the value 7 using the has method. It outputs whether the set contains the value or not. In this case, the output will be Array contains 7: true since 7 is present in the set.

2. Traversing a graph using depth-first search:

  • The graph object represents a graph with nodes and their corresponding neighbors.
  • The function dfs (depth-first search) takes a node as an input and recursively traverses the graph in a depth-first manner.
  • The console.log(node) statement prints the current node.
  • The for...of loop iterates over the neighbors of the current node and recursively calls dfs on each neighbor.
  • The line dfs('A'); starts the depth-first search from the node A .
  • The output will be the traversal path of the graph in a depth-first manner: A B D E C F G . Each node is printed as it is visited.

Linear Algebra is a branch of mathematics that deals with vector spaces and linear transformations. It is used extensively in computer graphics, machine learning, and data analysis. Understanding concepts such as matrices, determinants, and eigenvalues can help you build systems that process large amounts of data efficiently.

    
javascript
Copy
// Multiplying two matrices const a = [[1, 2], [3, 4]]; const b = [[5, 6], [7, 8]]; const multiplyMatrices = (a, b) => { const result = []; const n = a.length; const m = b[0].length; for (let i = 0; i < n; i++) { result[i] = []; for (let j = 0; j < m; j++) { let sum = 0; for (let k = 0; k < a[0].length; k++) { sum += a[i][k] * b[k][j]; } result[i][j] = sum; } } return result; }; const c = multiplyMatrices(a, b); console.log(c); // Output: [[19, 22], [43, 50]]

The above JavaScript code demonstrates multiplying two matrices a and b to obtain their product matrix c

  • The matrices a and b are defined as two-dimensional arrays. The matrix a has dimensions 2x2 , and the matrix b also has dimensions 2x2 .
  • The function multiplyMatrices takes two matrices a and b as input and returns their product matrix.
  • Inside the function:
    • An empty array result is initialized to store the result matrix.
    • The variables n and m are assigned the number of rows in matrix a and the number of columns in matrix b , respectively.
    • The first for loop iterates over the rows of matrix a .
      • Within this loop, an empty array is created within result to store the current row of the resulting matrix.
      • The second for loop iterates over the columns of matrix b .
        • Within this loop, a variable sum is initialized to accumulate the sum of products of corresponding elements from matrices a and b .
        • The third for loop iterates over the columns of matrix a (or rows of matrix b ).
          • The corresponding elements from matrices a and b are multiplied and added to sum .
        • The resulting sum is assigned to the corresponding element in the result matrix.
      • The resulting matrix result is returned.
  • The line const c = multiplyMatrices(a, b); calls the multiplyMatrices function with matrices a and b as arguments, storing the result in matrix c .
  • The line console.log(c); outputs the resulting matrix c , which is [[19, 22], [43, 50]] .

Number theory deals with the properties of numbers and is used in cryptography and security. Understanding concepts such as prime numbers, modular arithmetic, and the RSA algorithm can help you create secure systems and protect sensitive data.

It's the branch of mathematics that deals with the properties and behavior of numbers, especially integers. It has applications in various areas of computer science, such as cryptography, coding theory, and algorithms.

Here's an example of how number theory can be used to solve a programming problem.

Problem : Write a function to check if a given number is a prime number.

Solution : A prime number is a positive integer that has no positive integer divisors other than 1 and itself. We can use the following algorithm to determine if a number is prime:

  • If the number is less than 2 , it is not prime .
  • If the number is 2 , it is prime .
  • If the number is even , it is not prime .
  • Check if the number has any odd divisors less than its square root .
  • The last step is based on the fact that any non-prime number must have a divisor less than or equal to its square root.

Here's the JavaScript code to implement the above algorithm:

    
javascript
Copy
const isPrime = (n) => { if (n < 2) { return false; } else if (n == 2) { return true; } else if (n % 2 == 0) { return false; } else { const limit = Math.floor(Math.sqrt(n)); for (let i = 3; i <= limit; i += 2) { if (n % i == 0) { return false; } } return true; } }; console.log(isPrime(7)); // Output: true console.log(isPrime(15)); // Output: false

The isPrime function takes a number n as input and returns true if it is a prime number, and false otherwise. The function first checks if n is less than 2 , equal to 2 , or even , and returns the corresponding value. If n passes these tests, the function loops through odd numbers from 3 to the square root of n , checking if n is divisible by any of them. If n is divisible by any odd number, it is not a prime number and the function returns false . If no odd divisors are found, the function returns true .

Conclusions

These are just a few of the essential math concepts that a self-taught software engineer should study. While you may not need to become an expert in every area , having a basic understanding of these topics can help you build better software and solve complex problems. There are many resources available online, including textbooks, video courses, and interactive tutorials, that can help you learn these concepts and become a better programmer.

Luis Velazquez Bilbao
Software Engineer