# Calculating the value of e

```ruby
func calculate_e(n=50) {
    sum(0..n, {|k| 1/k! })
}

say calculate_e()
say calculate_e(69).as_dec(100)
```

## Output:

```
2.7182818284590452353602874713526624977572470937
2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427
```

For finding the number of required terms for calculating `e` to a given number of decimal places, using the formula `Sum_{k=0..n} 1/k!`, we have:

```ruby
func f(n) {
    var t = n*log(10)
    (n + 10).bsearch_le { |k|
        lngamma(k+1) <=> t
    }
}

for k in (1..10) {
    var n = f(10**k)
    say "Sum_{k=0..#{n}} 1/k! = e correct to #{10**k->commify} decimal places"
}
```

## Output:

```
Sum_{k=0..13} 1/k! = e correct to 10 decimal places
Sum_{k=0..69} 1/k! = e correct to 100 decimal places
Sum_{k=0..449} 1/k! = e correct to 1,000 decimal places
Sum_{k=0..3248} 1/k! = e correct to 10,000 decimal places
Sum_{k=0..25205} 1/k! = e correct to 100,000 decimal places
Sum_{k=0..205022} 1/k! = e correct to 1,000,000 decimal places
Sum_{k=0..1723507} 1/k! = e correct to 10,000,000 decimal places
Sum_{k=0..14842906} 1/k! = e correct to 100,000,000 decimal places
Sum_{k=0..130202808} 1/k! = e correct to 1,000,000,000 decimal places
Sum_{k=0..1158787577} 1/k! = e correct to 10,000,000,000 decimal places
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trizen.gitbook.io/sidef-lang/programming_tasks/c/calculating_the_value_of_e.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
