# How to calculate a weighted center of a set of zip codes [duplicate]

I have the zip codes for the US and their population. I need to calculate the weighted center. I have tried finding the zip code with the minimum

``````sum(distance_to_zip * zip_population)
``````

This works but is slow. With 30,000 zip codes there are two loops, so 900 million calculations. I've tried to optimize by sorting based on distance from the middle, and stopping the inner loop once the value hits the current lowest value. This helps, but it's still too slow.

Is there a faster algorithm for calculating the weighted center?

Note: I am writing my own code, I'm not looking to do this in a GIS package.

• You are trying to find the weighted center of every pair of zipcodes? IE, ~450 million (not 900 million because of symmetry) results?
– user1462
Aug 24, 2017 at 18:24
• I'm wanting to find the weighted center (i.e. center of gravity) of the contiguous USA based on zip code population (not area). Aug 24, 2017 at 18:40
• The formula given on ArcGIS' help looks optimal: desktop.arcgis.com/en/arcmap/10.3/tools/…
– Tom
Aug 24, 2017 at 18:57
• Why total(x*pop)/total(pop) to define longitude and similar with longitude is not working? Aug 24, 2017 at 19:00
• You don't need looping calculations for that. Start with any zip code centroid and go from there. The US center of population is already know, you'll get a slightly different result because you're using zipcodes, but it'll be essentially the same.
– user1462
Aug 24, 2017 at 19:28