## Calculating Euler's Number

We are well aware of Euler’s famous number,

$$e = 2.71828182845$$

There are also quite a few ways of deriving Euler’s Number. There’s the Taylor expansion method:

$$e = \sum_{k = 0} ^{\infty} \frac{1}{k!}$$

There is also the classical limit form:

$$e = \lim_{n \rightarrow \infty} \left( 1 + \frac{1}{n} \right)^n$$

Then there is another. Let $R$ be a random number generated between $[0, 1]$, inclusive. Then $e$ is the average of the number of $R$s it takes to sum greater than $1$.

With more rigor, for uniform $(0,, 1)$ random independent variables $R_1$, $R_2$, $\ldots$, $R_n$,

$$N = \min \left( n: \sum _{k = 1} ^n R_i > 1 \right)$$

where

$$e = \text{Average}(n)$$

The proof can be found here, but it’s pretty math-heavy. Instead, an easier method is to write a program to verify for large enough $n$.

n | Sum Solution | Limit Solution | Random Uniform Variable |
---|---|---|---|

1 | 1 | 2 | 2 |

10 | 1.7182818011 | 2.5937424601 | 2.5 |

100 | 1.7182818284 | 2.7048138294 | 2.69 |

1000 | 1.7182818284 | 2.7169239322 | 2.717 |

10000 | 2.7181459268 | 2.7242 | |

100000 | 2.7182682371 | 2.71643 | |

1000000 | 2.7182804690 | 2.71961 | |

10000000 | 2.7182816941 | 2.7182017 | |

100000000 | 2.7182817983 | 2.71818689 | |

1000000000 | 2.7182820520 | 2.718250315 |

## Source Code

```
def e_sum(upper_bound):
if upper_bound < 1:
return 0
return Decimal(1.0) / Decimal(math.factorial(upper_bound)) + Decimal(e_sum(upper_bound - 1))
def e_limit(n):
return Decimal((1 + 1.0 / float(n))**n)
def find_greater_than_one(value=0, attempts=0):
if value <= 1:
return find_greater_than_one(value + random.uniform(0, 1), attempts + 1)
return attempts
```