sergazy.nurbavliyev@gmail.com © 2021

## Can you form a triangle ?

Question: Assume we have a stick of length 1. Suppose you randomly break it into three parts. What is the probability that you can form a triangle using these three parts?

### Intuition

I always like to break into the cases. The idea is divide and conquer :)
Let us figure out what these cases are. First, assume we label these three pieces as $a,b$ and $c.$ Remember $a+b+c=1$. We know that if any of these pieces is bigger than $\frac{1}{2}$ than it is impossible to form a triangle. There are 3 such cases. Now the last case is if all of them are strictly smaller than  $\frac{1}{2}.$ In that case it is possible to form a triangle. You can now intuitively think that all of these outcomes have an equal probability. Thus the answer is 1/4. To make things clear I show these argument in a table.

| Cases:       | a              |b                |      c          | Triangle ?    | 
|--------------| -------------- |-----------------| ----------------|---------------|
|Posssibility 1| a>1/2 |b<1/2| c<1/2|No |
|Posssibility 2|       a<1/2 |b>1/2| c<1/2|No |
|Posssibility 3|        a<1/2 |b<1/2| c>1/2|No |
|Posssibility 4|         a<1/2 |b<1/2| c<1/2|Yes |


```python

```

## Theoritical result

I would like to label the breaking points as $x$ and $y$. So there are two possibilities:
Case 1: $x < y$ and Case 2: $y < x$

Case 1:$x < y$: Length of pieces after choosing points x and y: would be
$x , (y-x) , (1-y)$ 

We showed the partition below.

0----- $x$-------$y$------1

There are 3 possible combination for satisfying triangle inequality.

\begin{equation}
x + (y-x)  > (1-y)\Rightarrow 2y > 1\Rightarrow  y > (1/2)
\end{equation}

\begin{equation}
 x + (1-1)  > (y-x)\Rightarrow  2x + 1 > 2y\Rightarrow  y < x + (1/2)
\end{equation}

\begin{equation}
(y-x) + (1-1)  > x\Rightarrow  2x < 1 \Rightarrow  x < 1/2
\end{equation}

I showed in a figure below the common area of these three region. 


```python
from IPython.display import Image
Image(filename='triangle.jpeg')
```




    

    



Case 2:$y < x$: Length of pieces after choosing points x and y: would be
$y , (x-y) , (1-x)$. With the same logic, we would get  $\frac{1}{8}$

Now we can add the results to get the probability of forming triangle. That would be  $\frac{1}{8}+ \frac{1}{8}=\frac{1}{4}.$

## Python code for simulation


```python
import random
def forms_triangle(a, b, c):
    return a + b > c and a + c > b and b + c > a
```


```python
def counts(trials):
    num_tri = 0
    for i in range(trials):
        x, y = random.uniform(0, 1.0), random.uniform(0, 1.0)
        if x > y:
            x, y = y, x
            if forms_triangle(x, y - x, 1 - y):
                num_tri += 1
        else:
            x, y = x, y
            if forms_triangle(x, y - x, 1 - y):
                num_tri += 1
            
    return num_tri
```


```python
trials = 1000000
formed_triangles = counts(trials)
```


```python
formed_triangles/trials
```




    0.249959




```python

```

ab5b82f54937c4bebb6426bdb62d0b322bf704be
ipynb
Jupyter Notebook
Can you form a triangle March 5, 2021.ipynb
sernur/probability_stats_interveiw_questions
3144dae00fa83c82ff4e1f7668828349270a1937
MIT
2021-03-04T06:48:28.000Z
2021-04-19T10:04:24.000Z
Can you form a triangle March 5, 2021.ipynb
sernur/probability_stats_interveiw_questions
3144dae00fa83c82ff4e1f7668828349270a1937
MIT
2021-03-05T22:00:43.000Z
2021-03-05T22:00:43.000Z
Can you form a triangle March 5, 2021.ipynb
sernur/probability_stats_interveiw_questions
3144dae00fa83c82ff4e1f7668828349270a1937
MIT
2021-03-04T05:02:00.000Z
2021-03-16T01:13:40.000Z
Qwen/Qwen-72B
1. YES
2. YES