-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler010.pro
46 lines (41 loc) · 1.28 KB
/
euler010.pro
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
34
35
36
37
38
39
40
41
42
43
44
45
46
% Problem 10: Summation of primes
% -------------------------------
% The sum of all primes below 10 is 17.
% Find the sum of all primes below 2000000.
%
% =============================================================================
%
% The perfect job for an Erathosthenes sieve. Ancient greek knowledge to the
% rescue!
%
% Implementation notes:
% - Low performance, high CPU usage and high stack space consumption. Prime
% numbers are hard;
% - This is really an Euler's sieve: the k-th recursion leaves (after
% subtract/3) only numbers coprime with the first k primes.
/** <examples>
?- euler010(2000000,X).
*/
:- use_module(library(clpfd)).
:- use_module(library(yall)).
:- use_module(library(lists),[numlist/3,sum_list/2,subtract/3]).
:- use_module(library(apply),[convlist/3]).
:- use_module(library(statistics),[time/1]).
test:-
write("Testing for "),
write(euler010(2000000,142913828922)),
writeln(" takes too long. A shorter test:"),
writeln(euler010(20000,21171191)),
time(euler010(20000,21171191)).
euler010(L,SP):-
L in 2..sup,
numlist(2,L,LN),
sieve(L,LN,LP),
sum_list(LP,SP).
sieve(X,[H|P],[H|P]):-
H*H #> X,
!.
sieve(X,[H|TN],[H|TP]):-
convlist({H,X}/[N,D]>> (D #= H*N, X #>= D),[H|TN],LD),
subtract(TN,LD,R),
sieve(X,R,TP).