Summation of primes: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Go)
Line 6: Line 6:
<br>Find the sum of all the primes below '''two million'''
<br>Find the sum of all the primes below '''two million'''
<br><br>
<br><br>

=={{header|Go}}==
{{libheader|Go-rcu}}
<lang go>package main

import (
"fmt"
"rcu"
)

func main() {
sum := 0
for _, p := range rcu.Primes(2e6 - 1) {
sum += p
}
fmt.Printf("The sum of all primes below 2 million is %s.\n", rcu.Commatize(sum))
}</lang>

{{out}}
<pre>
The sum of all primes below 2 million is 142,913,828,922.
</pre>


=={{header|Ring}}==
=={{header|Ring}}==

Revision as of 08:36, 8 November 2021

Summation of primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task


The task descrition is taken from Project Euler (https://projecteuler.net/problem=10)
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17
Find the sum of all the primes below two million

Go

Library: Go-rcu

<lang go>package main

import (

   "fmt"
   "rcu"

)

func main() {

   sum := 0
   for _, p := range rcu.Primes(2e6 - 1) {
       sum += p
   }
   fmt.Printf("The sum of all primes below 2 million is %s.\n", rcu.Commatize(sum))

}</lang>

Output:
The sum of all primes below 2 million is 142,913,828,922.

Ring

<lang ring> load "stdlib.ring" see "working..." + nl sum = 0 limit = 2000000

for n = 2 to limit

   if isprime(n)
      sum += n
   ok

next

see "The sum of all the primes below two million:" + nl see "" + sum + nl see "done..." + nl </lang>

Output:
working...
The sum of all the primes below two million:
142,913,828,922
done...

Wren

Library: Wren-math
Library: Wren-fmt

<lang ecmascript>import "./math" for Int, Nums import "./fmt" for Fmt

Fmt.print("The sum of all primes below 2 million is $,d.", Nums.sum(Int.primeSieve(2e6-1)))</lang>

Output:
The sum of all primes below 2 million is 142,913,828,922.