Home > Back-end >  Finding distinct numbers in a range, given some step size
Finding distinct numbers in a range, given some step size

Time:10-25

I have been trying to come up with some mathematical formula to find the distinct numbers in a given range with a variable step size.

First I tried in 1-D, like in a range [0,10], with a step size of 0.1. Solution: In between 0.1-1,1.1-2,.......,9.1-10. Every sub-range has 10 numbers, so total #=100. Including 0, we get 100 1=101.

Next is to find the total points in a grid (2D). Solution: In a grid, with two axes x and y, we need to find the total # points. x is in the range [-10,10] and y in the range [-10,10], with a step size of 0.1. With 2D it gets complicated, and in higher dimensions and with varying step size, it becomes really messy. I was just wondering if there is a generalized formula or method to find the points.

Grid or the 2D space looks something like this

CodePudding user response:

It's not significantly more complicated in higher dimensions, because the optimal packing in higher dimensions is a grid. Suppose you have x in the closed range [x1, x2] in steps of dx and you have y in the closed range [y1, y2] in steps of dy. Just on the x axis, there are 1 floor((x2 - x1) / dx) values, and just on the y axis there are 1 floor((y2 - y1) / dy) values, so in the 2d grid there are

(1   floor((x2 - x1) / dx)) * (1   floor((y2 - y1) / dy))

distinct points.

This generalizes to higher dimensions (assuming dx, dy, d... are constant). So just find the number of distinct points in each dimension and multiply those values together to get the number of distinct points in the n-dimensional region.

  • Related