Algorithm to compact/simplify ranges?
Make a list of all $2n$ beginning and ending points of the $n$ ranges.  Record each as a (position, type) pair.
Sort this list by position, with starting points comparing below ending points when positions are equal.
Loop through the sorted list, incrementing a counter variable (initially zero) whenever you see a beginning point and decrementing it whenever you see an ending point.  On a 0->1 transition of this variable, output the current position as a starting point.  On a 1->0 transition, output the current position as an ending point.