Common Useful Formulas - robbiehume/CS-Notes GitHub Wiki

  • Sum of n natural numbers starting from 1 ([1, n]): n * (n + 1) / 2

    • Example 1 -> 5: (5 * 6)/2 = 15
  • Sum of sequence a to b: (n/2) * (a + b)

    • Where n is the number of terms in the sequence (b - a + 1)
    • Example 3 -> 9: (7/2) * (3 + 9) = 42
    • Or if you don't want to remember that, you can do subtract sequence of 1 -> 2 from 1 -> 9:
      • 1 -> 9: 9 * (9 + 1) / 2 = 45
      • 1 -> 2: 2 * (2 + 1) / 2 = 3
      • 3 -> 9: 45 - 3 = 42