# Primes whose sum of digits is 25

Simple solution:

```ruby
5000.primes.grep { .sumdigits == 25 }.say
```

Generate such primes from digits (asymptotically faster):

```ruby
func generate_from_prefix(limit, digitsum, p, base, digits, t=p) {
 
    var seq = [p]
 
    digits.each {|d|
        var num = (p*base + d)
        num <= limit    || return seq
 
        var sum = (t + d)
        sum <= digitsum || return seq
 
        seq << __FUNC__(limit, digitsum, num, base, digits, sum)\
               .grep { .is_prime }...
    }
 
    return seq
}
 
func primes_with_digit_sum(limit, digitsum = 25, base = 10, digits = @(^base)) {
    digits.grep { _ > 0 }\
          .map  { generate_from_prefix(limit, digitsum, _, base, digits)... }\
          .grep { .sumdigits(base) == digitsum }\
          .sort
}
 
say primes_with_digit_sum(5000)
```

#### Output:

```
[997, 1699, 1789, 1879, 1987, 2689, 2797, 2887, 3499, 3697, 3769, 3877, 3967, 4597, 4759, 4957, 4993]
```


---

# 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/p/primes_whose_sum_of_digits_is_25.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.
