# Summation of primes

Built-in:

```ruby
say sum_primes(2e6)  #=> 142913828922
```

Linear algorithm:

```ruby
func sum_primes(limit) {
    var sum = 0
    for (var p = 2; p < limit; p.next_prime!) {
        sum += p
    }
    return sum
}
 
say sum_primes(2e6)
```

Sublinear algorithm:

```ruby
func sum_of_primes(n) {
 
    return 0 if (n <= 1)
 
    var r = n.isqrt
    var V = (1..r -> map {|k| idiv(n,k) })
    V << range(V.last-1, 1, -1).to_a...
 
    var S = Hash(V.map{|k| (k, polygonal(k,3)) }...)
 
    for p in (2..r) {
        S{p} > S{p-1} || next
        var sp = S{p-1}
        var p2 = p*p
        V.each {|v|
            break if (v < p2)
            S{v} -= p*(S{idiv(v,p)} - sp)
        }
    }
 
    return S{n}-1
}
 
say sum_of_primes(2e6)
```

#### Output:

```
142913828922
```


---

# 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/s/summation_of_primes.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.
