I am a software Engineer and I've run into an issue and I need to generate a kind of formula that will help me calculate iterations.

I thought of asking this question on stackoverflow but what I need here is a formula that can help me.

**Note: I am really interested in understanding the process of forming a formula like this. If you can, please refer me to a math topic online that I can study to help me generate a formula like this myself next time.**

Here is the Basics

* The codes work really well right now so all I need know is a formula. (maybe something like series or permutation ).
* I am building an API that can take two zip codes, and then find the distance between them.
* It works great using Google's geocoding APIs but now I want to build a data warehouse myself, cache it how I want and then be able to provide some services with it.
* The idea is to expose some endpoints that can be hit with two zip codes and then the distance between them returned (ignoring the road paths which was an advantage I get with the google API).
* Am building the API with nodejs and I use the haversine formula for the calculation.
* Currently running the code on my Linux machine while storing the data in MYSQL database.
* Later, I will running the code as AWS Lamda function while saving the data in RDS Mysql.

The Problem.

* The problem is, the number of iteration I run to calculate the distance between each zip code increases as the number of zip codes increases.
* Note: I'll run this just ones (to generate the distances) and then for subsequent new zip codes that might be added in the future, the iterations will be minimal.
* Say you have 4 zip codes,
* Then you calculate the distance between

	+ 1 and 2, 1 and 3, 1 and 4,
	+ 2 and 3, 2 and 4
	+ 3 and 4
* So this will require 6 iterations.

However, say you have 10 zip codes, then you will calculate distances between

* 1 and 2, 1 and 3, 1 and 4, 1 and 5, 1 and 6, 1 and 7, 1 and 8, 1 and 9, 1 and 10,
* 2 and 3, 2 and 4, 2 and 5, 2 and 6, 2 and 7, 2 and 8, 2 and 9, 2 and 10,
* 3 and 4, 3 and 5, 3 and 6, 3 and 7, 3 and 8, 3 and 9, 3 and 10,
* 4 and 5, 4 and 6, 4 and 7, 4 and 8, 4 and 9, 4 and 10,
* 5 and 6, 5 and 7, 5 and 8, 5 and 9, 5 and 10,
* 6 and 7, 6 and 8, 6 and 9, 6 and 10,
* 7 and 8, 7 and 9, 7 and 10,
* 8 and 9, 8 and 10,
* 9 and 10,
And that will be about 45 iterations, up from 6 iterations when we had 4 zip codes.

How can I form a formula to calculate this by just plugin in some values and do the calculations.

Currently I have a typescript method that does the calculation but it has to loop through continuously.

```
  private calculateIterations(start: number = 1, total: number = 300) {
    // Holds the number of iterations.
    let count = 0;
    // Loop through {total} times.
    for (let a = 0; a < total; a++) {
      // For each item, loop through the rest of the items.
      for (let b = (a + 1); b < total; b++) {
        // Increment the count for each sub iteration.
        count++;
        console.log('count = ', count, ' | a = ', a, ' b = ', b);
      }
    }
  }

```