Project-Euler-010

Problem


The sum of the primes below 10 is $2 + 3 + 5 + 7 = 17$.


Find the sum of all the primes below two million.

Answer

1
142913828922

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python
from itertools import takewhile

def eratosthenes():
'''Yields the sequence of prime numbers via the Sieve of Eratosthenes.'''
D = {} # map composite integers to primes witnessing their compositeness
q = 2 # first integer to test for primality
while 1:
if q not in D:
yield q # not marked composite, must be prime
D[q*q] = [q] # first multiple of q not already marked
else:
for p in D[q]: # move each witness to its next multiple
D.setdefault(p+q,[]).append(p)
del D[q] # no longer need D[q], free memory
q += 1

print(sum(takewhile(lambda x: x < 2000000, eratosthenes())))


JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const sieve = {}
let s = 0
for (let q = 2; q < 2000000; q++) {
if (sieve[q]) {
sieve[q].forEach((p) => {
const list = sieve[p + q] || []
list.push(p)
sieve[p + q] = list
})
delete sieve[q]
} else {
s += q
sieve[q * q] = [q]
}
}
console.log(s)


Go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main

import "fmt"
import "math"

func PrimeSieve(n int64) []int64 {
result := make([]int64, 0, n/int64(math.Log(float64(n))))
sieve := make([]bool, n+1)
sn := int64(math.Sqrt(float64(n)))
var i, j int64
for i = 2; i <= sn; i++ {
if !sieve[i] {
for j = i * i; j <= n; j += i {
sieve[j] = true
}
}
}
for i = 2; i <= n; i++ {
if !sieve[i] {
result = append(result, i)
}
}
return result
}

func main() {
primes := PrimeSieve(2000000)
var sum int64 = 0
for _, p := range primes {
sum += p
}
fmt.Println(sum)
}


Ruby

1
2
3
#!/usr/bin/env ruby
require 'mathn'
puts Prime.take_while{ |n| n < 2000000 }.reduce(:+)


Haskell

1
2
3
4
5
6
7
primes :: [Integer]
primes = 2 : sieve primes [3,5..] where
sieve (p:ps) xs = h ++ sieve ps [x | x <- t, rem x p /= 0]
where (h, t) = span (< p*p) xs

main :: IO ()
main = print $ sum $ takeWhile (< 2000000) primes


Clojure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env clojure
(defn primes [n]
(defn improve [p nums]
(filter #(or
(not (= (rem % p) 0))
(= % p))
nums))
(defn prime-iter [p nums i]
(if (> (* p p) n)
nums
(prime-iter (nth nums (+ i 1)) (improve (nth nums (+ i 1)) nums) (+ i 1))))
(prime-iter 2 (range 2 (+ n 1)) -1))

(println (reduce + (primes 2000000)))


Mathematica

1
2
(* Use Mathematica's built-in functions for easy problem-solving. *)
Total[Select[Range[1999999], PrimeQ]]


Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public final class p010 implements EulerSolution {

public static void main(String[] args) {
System.out.println(new p010().run());
}


/*
* Call the sieve of Eratosthenes and sum the primes found.
* A conservative upper bound for the sum is 2000000^2, which fits in a Java long type.
*/
private static final int LIMIT = 2000000;

public String run() {
long sum = 0;
for (int p : Library.listPrimes(LIMIT - 1))
sum += p;
return Long.toString(sum);
}

}

文章作者: Monad Kai
文章链接: onlookerliu.github.io/2018/03/01/Project-Euler-010/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Code@浮生记
支付宝打赏
微信打赏