you should not approach this task with a brute force of the CPU, because you will surely get stuck.

I can make a couple of suggestions to improve the algorithm:

1 - use GPU to do your calculations.
2 - cluster your space (each cluster can contain hundreds of particles). calculate the total mass and the center of gravity of each cluster. attract individual particles with each other within a cluster (or you might create sub-clusters if you have too many particles). attract individual particles within a cluster to the center of mass of the other clusters. this will significantly decrease the time complexity of your calculations (at the moment you have O(N) = N * N).