偷懒一把,用Matlab计算一行搞定:
n = 2000000;
result = sum(primes(n))
得到的结果是:1.42913828922e+011
思路: primes(n) 是返回所有不大于n的素数的数组, 然后通过sum对这个数组求和。
prime(n) 的算法是:
1. 得到一个数组A= [1, 3, 5, ...., n - 1];
2. A[0] = 2;
3. 开始遍历A:将整数A[i]所有数字都置零;
4. 然后对将所有为0的元素擦除。
MatLab之primes代码:
function p = primes(n)
%PRIMES Generate list of prime numbers.
% PRIMES(N) is a row vector of the prime numbers less than or
% equal to N. A prime number is one that has no factors other
% than 1 and itself.
%
% Class support for input N:
% float: double, single
%
% See also FACTOR, ISPRIME.
% Copyright 1984-2004 The MathWorks, Inc.
% $Revision: 1.16.4.2 $ $Date: 2004/07/05 17:02:09 $
if length(n)~=1
error('MATLAB:primes:InputNotScalar', 'N must be a scalar');
end
if n < 2, p = zeros(1,0,class(n)); return, end
p = 1:2:n;
q = length(p);
p(1) = 2;
for k = 3:2:sqrt(n)
if p((k+1)/2)
p(((k*k+1)/2):k:q) = 0;
end
end
p = p(p>0);