Summarize primes: Difference between revisions

Line 1,064:
<pre>
Found 21 of em: 1, 2, 4, 6, 12, ..., 132, 146, 152, 158, 162
</pre>
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">isPrime(2).
isPrime(N):-
between(3, inf, N),
N /\ 1 > 0, % odd
M is floor(sqrt(N)) - 1, % reverse 2*I+1
Max is M div 2,
forall(between(1, Max, I), N mod (2*I+1) > 0).
 
primeSum([], _, _, []).
primeSum([P|PList], Index, Acc, [Index|CList]):-
Sum is Acc + P,
isPrime(Sum),!,
format('~|~t~d~3+ ~|~t~d~3+ ~|~t~d~5+', [Index, P, Sum]),nl,
Index1 is Index + 1,
primeSum(PList, Index1, Sum, CList).
primeSum([P|PList], Index, Acc, CntList):-
Index1 is Index + 1,
Sum is Acc + P,
primeSum(PList, Index1, Sum, CntList).
 
do:-Limit is 1000,
numlist(1, Limit, List),
include(isPrime, List, PrimeList),
primeSum(PrimeList, 1, 0, CntList),
length(CntList, Number),
format('~nfound ~d such primes~n', [Number]).</syntaxhighlight>
{{out}}
<pre>?- do.
1 2 2
2 3 5
4 7 17
6 13 41
12 37 197
14 43 281
60 281 7699
64 311 8893
96 503 22039
100 541 24133
102 557 25237
108 593 28697
114 619 32353
122 673 37561
124 683 38921
130 733 43201
132 743 44683
146 839 55837
152 881 61027
158 929 66463
162 953 70241
 
found 21 such primes
</pre>
 
64

edits