I am writing a computer program/algorithm to count the number of integer points inside a sphere of radius R and Dimension D centered at the origin. In essence, if we have a sphere of dimension 2 (circle) and radius 5, I am determining all integer solutions to the inequality X^2+Y^2 ≤ 25. Instead of counting every possible integer point, is there an efficient way to count the points ? Using symmetry ?
-
2@Sean Cline: The question regards counting _all_ integer points within the sphere, roughly equal to its volume. I should think it is more of a question of (discrete) mathematics. I wouldn't be surprised if there was a clever analytic (`O(1)`) formula for this. – doynax May 24 '16 at 10:38
-
Why do you need new question similar to existing one? Edit old question, add new thoughts and tags, if you want. – MBo May 24 '16 at 12:06
2 Answers
Suppose the dimension is 3, and R is the radius. For z = 0, the possible x,y coordinates are the points inside a circle of radius R, and for any z, the x,y's are the points inside a circle of radius sqrt( R * R - z * z); the possible values of z are -r, .. 0, 1, .. r where r is the smallest integer less than R. Note the count for z and -z will be the same.
When we get down to dimension 1, we are asking how many integers i satisfy |i| < R, and this is 1 + 2 * r
So:
int ncip( int dim, double R)
{
int i;
int r = (int)floor( R);
if ( dim == 1)
{ return 1 + 2*r;
}
int n = ncip( dim-1, R); // last coord 0
for( i=1; i<=r; ++i)
{ n += 2*ncip( dim-1, sqrt( R*R - i*i)); // last coord +- i
}
return n;
}

- 4,211
- 2
- 14
- 12
Well, using symmetry, you can always just count all the integer solutions on the positive side, and then invert the components. So, in case of your Circle (D=2, R=5), you'd just have to find
{ X,Y ∈ N: X²+Y² ≤ R }. Then create the combinations (X,Y), (X,-Y), (-X,Y), (-X,-Y)
This leaves you with just (1/2)D solutions to find.
Also, you can approximate a circle of radius R as a Square with radius R/√2, so all combinations of integers smaller than or equal to that can automatically be added.

- 21
- 2