# Union and Intersection of intervals

I have two sets $A = \{1 \leq x \leq 5\}$ and $B = \{5 \leq x \leq 8\}$. Now I want to find the `Union` and `Intersection` of $A$ and $B$.

I tried `Union[A, B]`, I got `{1 <= x <= 5, 5 <= x <= 8}` and for `Intersection[A, B]`, I got `{}`. The correct answer for $A \cup B$ is `[1, 8]` and $A \cap B$ is `{5}`. How do I tell Mathematica to do that?

And if $A = \{1 < x < 5\}$ and $B = \{x > 5\}$. Now I want to find the `Union` and `Intersection` of $A$ and $B$. How do I tell Mathematica to do that?

-

1
You will want to use `Interval`. – David Carraher Oct 1 '12 at 1:13

## 1 Answer

````a = Interval[{1, 5}];
b = Interval[{5, 8}];
IntervalUnion[a, b]
````

Interval[{1, 8}]

````IntervalIntersection[a, b]
````

Interval[{5, 5}]

-
Thank you very much. – minthao_2011 Oct 1 '12 at 1:23

2
`Interval[{1,5},{5,8}]` also gives `IntervalUnion[a,b]` (+1) – kguler Oct 1 '12 at 1:28
Nice. I wasn't aware of that. – David Carraher Oct 1 '12 at 1:30

1
@minthao: `B = Interval[{1, ∞}]`. – J. M.♦ Oct 1 '12 at 1:52

1
@minthao. Mathematica seems to work only with closed intervals. `Interval[{1,5]` includes the numbers 1 and 5, and therefore does not match your original inequality, 1<x<5. I don't know whether there is a way to not include the end points. Perhaps this is a question worth asking. – David Carraher Oct 1 '12 at 2:31
show 10 more comments
lang-mma
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 10, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9314320683479309, "perplexity_flag": "middle"}