I want to run a Barnes Hut N-body simulation (Wikipedia article). I know how much memory a single node of the octree will occupy, but I can't figure out what a worst case memory usage scenario would be for a given number of particles. In short, I'm wondering how many nodes could possibly exist in the octree for a given number of particles. I need to know this to know how much memory to allocate for the octree.
Edit:
Oh, and I'm writing in C if anybody wants to give me code instead of just and explanation.
I have this, which is way worse than a worst case scenario. But it's guaranteed to AT LEAST allocate enough memory. I'm hoping somebody can give me something a little more memory efficient.
int KBH_worstcase(int particles)
{ // returns number of nodes required to store a number of particles in a worst case scenario
int worst;
unsigned int n;
worst=1;
n=1;
while(n<particles)
{
n=n<<3;
worst+=n;
}
return worst;
}