7
$\begingroup$

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!

  • 0
    @Thunder: This is still not correct. But nevermind, I hope that whoever reads the question will look at my answer and figure out the correct meaning anyway.2010-12-29

1 Answers 1

11

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.

  • 0
    yes this the correct answer ... thanks2010-12-29