Prime numbers p for which the sum of primes less than or equal to p is prime: Difference between revisions

Line 457:
</pre>
=={{header|Prolog}}==
runs with swi-prolog
<syntaxhighlight lang="peologprolog">
primes(2, Limit):- 2 =< Limit.
primes(3, Limit):- 3 =< Limit.
primes(N, Limit):-
between(5, Limit, N),
N /\ 1 > 0, % odd
N mod 3 > 0, % /= 3*i
M is floor(sqrt(N)) + 1, % reverse 6*I-1
Max is M div 6,
forall(between(1, Max, I), (N mod (6*I-1) > 0, N mod (6*I+1) > 0)).
 
isPrime(N):-
primes(N, inf).
 
primeSum(List, LastP):-
append(SubList, _, List),
sum_list(SubList, Sum),
isPrime(Sum),
last(SubList, LastP).
 
showList(List):-
last(List, Last),
FmtLen is 2 + floor(log10(Last)), % one more for space
swritef(FmtStr, '%%dr', [FmtLen]),
findnsols(10, X, (member(X, List), writef(FmtStr, [X])), _), nl,
fail.
showList(_).
 
do(Limit):-
findall(N, primes(N, Limit), PrimeList),
findall(LastP, primeSum(PrimeList, LastP), SumList),
showList(SumList).
 
do:- do(2000).
</syntaxhighlight>
{{out}}
<pre>
?- do.
2 3 7 13 37 43 281 311 503 541
557 593 619 673 683 733 743 839 881 929
953 1061 1163 1213 1249 1277 1283 1307 1321 1949
true.
</pre>
 
=={{header|Raku}}==
64

edits