How to find the intersection between a set of two ranges of number.

let me explain the question with an example,

{2,3} {3,8} would result to 0
{2,5} {3,8} would result to 2
{3,6} {3,8} would result to 3
{4,5} {3,8} would result to 1
{4,5} {3,8} would result to 1
{8,9} {3,8} would result to 0

Note:This could be easily done with series of if in a computer application, I am hungry for a mathematical solution!

  • $\begingroup$ By "intersection", you mean the number of "units" of overlap between the two given intervals? $\endgroup$ – J. M. is a poor mathematician Dec 29 '10 at 9:08
  • 1
    $\begingroup$ What are "tow ranges"? $\endgroup$ – Pete L. Clark Dec 29 '10 at 9:13
  • $\begingroup$ @ J.M yes , and @ Pete L.Clark I mean set of two numbers $\endgroup$ – Thunder Dec 29 '10 at 9:15
  • 1
    $\begingroup$ There is a great grief about this notation of yours, $(3,8)$ usually indicates all the real numbers which are strictly greater than $3$ and strictly smaller than $8$. Not just the integers, which is what seems to be your meaning. $\endgroup$ – Asaf Karagila Dec 29 '10 at 9:20
  • $\begingroup$ @Asaf Karagila I have changed to {,} I am new so not sure this is correct ,thanks $\endgroup$ – Thunder Dec 29 '10 at 9:41

If I understood you correctly, you have $\{x,x+1,\ldots,x+n\}$ and $\{y,y+1,\ldots,y+k\}$ and granted $x,y$ are integers you want to find out the number of elements in the intersections (i.e. how many numbers appear in both sets).

Well, take: $$\max\{0,\min\{y+k,x+n\}-\max\{x,y\}\}$$ and that should be it.

  • $\begingroup$ well the result is not a set but a number , that represents the value in the intersection set. $\endgroup$ – Thunder Dec 29 '10 at 9:40
  • 1
    $\begingroup$ @Thunder: Yes, and Asaf's construction does return the number you (seem to) want. $\endgroup$ – J. M. is a poor mathematician Dec 29 '10 at 9:50
  • $\begingroup$ yes this the correct answer ... thanks $\endgroup$ – Thunder Dec 29 '10 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.