# Least m such that n m is prime

```ruby
with (50) {|N|
    say "Least positive m such that n! + m is prime (first #{N}):"
    ^N -> map {|n|
        var f = n!; 1..Inf -> first {|k| f+k -> is_prime }
    }.each_slice(10, {|*s|
        say s.map{ '%3s' % _ }.join(' ')
    })
}

say ''; var prev = 0
for n in (1..5 -> map { 1e3*_ }) {
    var m = (prev..Inf -> lazy.map{|k|
        var f = k!; [k, f.next_prime - f]
    }.first {|k|
        k.tail >= n
    })
    say "First m > #{n} is #{m.tail} at position #{m.head}"
    prev = m.head
}
```

#### Output:

```
Least positive m such that n! + m is prime (first 50):
  1   1   1   1   5   7   7  11  23  17
 11   1  29  67  19  43  23  31  37  89
 29  31  31  97 131  41  59   1  67 223
107 127  79  37  97  61 131   1  43  97
 53   1  97  71  47 239 101 233  53  83

First m > 1000 is 1069 at position 107
First m > 2000 is 3391 at position 192
First m > 3000 is 3391 at position 192
First m > 4000 is 4943 at position 284
First m > 5000 is 5233 at position 384
```


---

# 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/l/least_m_such_that_n_m_is_prime.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.
