> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/l/largest_difference_between_adjacent_primes.md).

# Largest difference between adjacent primes

```ruby
func prime_gap_records(upto) {
 
    var gaps = []
    var p = 2
 
    each_prime(p.next_prime, upto, {|q|
        gaps[q-p] := p
        p = q
    })
 
    gaps.grep { defined(_) }
}
 
var upto   = 1e8
var primes = prime_gap_records(upto)
 
for n in (2 .. upto.ilog10) {
 
    var b = primes.last_by {|p| p < 10**n } \\ break
 
    printf("Largest prime gap up to 10^%s is %3s between %s and %s\n",
        n, b.next_prime - b, b, b.next_prime)
}
```

#### Output:

```
Largest prime gap up to 10^2 is   8 between 89 and 97
Largest prime gap up to 10^3 is  20 between 887 and 907
Largest prime gap up to 10^4 is  36 between 9551 and 9587
Largest prime gap up to 10^5 is  72 between 31397 and 31469
Largest prime gap up to 10^6 is 114 between 492113 and 492227
Largest prime gap up to 10^7 is 154 between 4652353 and 4652507
Largest prime gap up to 10^8 is 220 between 47326693 and 47326913
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://trizen.gitbook.io/sidef-lang/programming_tasks/l/largest_difference_between_adjacent_primes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
