Prime numbers p for which the sum of primes less than or equal to p is prime: Difference between revisions
Prime numbers p for which the sum of primes less than or equal to p is prime (view source)
Revision as of 22:29, 4 April 2024
, 1 month agoAdded Uiua solution
Drkameleon (talk | contribs) (added Arturo) |
(Added Uiua solution) |
||
(9 intermediate revisions by 8 users not shown) | |||
Line 136:
1-999: 21
</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
<syntaxhighlight lang="Delphi">
procedure ShowPrimeLesserSum(Memo: TMemo);
var N,Sum,Cnt: integer;
var S: string;
begin
Cnt:=0;
Sum:=0;
for N:=2 to 1000-1 do
if IsPrime(N) then
begin
Sum:=Sum+N;
if IsPrime(Sum) then
begin
Inc(Cnt);
S:=S+Format('%4d',[N]);
If (Cnt mod 5)=0 then S:=S+CRLF;
end;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Count='+IntToStr(Cnt));
end;
</syntaxhighlight>
{{out}}
<pre>
2 3 7 13 37
43 281 311 503 541
557 593 619 673 683
733 743 839 881 929
953
Count=21
Elapsed Time: 2.006 ms.
</pre>
=={{header|F_Sharp|F#}}==
Line 257 ⟶ 299:
=={{header|J}}==
<syntaxhighlight lang="j">(
{{out}}
<pre>2 3 7 13 37 43 281 311 503 541 557 593 619 673 683 733 743 839 881 929 953</pre>
Line 354 ⟶ 396:
953
</pre>
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
isPrime = function(n)
if n <= 3 then return n > 1
if n % 2 == 0 or n % 3 == 0 then return false
i = 5
while i ^ 2 <= n
if n % i == 0 or n % (i + 2) == 0 then return false
i += 6
end while
return true
end function
primes = []
sum = 0
for n in range(2, 1000)
if isPrime(n) then
sum += n
if isPrime(sum) then primes.push(n)
end if
end for
print primes.len + " found: " + primes
</syntaxhighlight>
{{out}}
<pre>
21 found: [2, 3, 7, 13, 37, 43, 281, 311, 503, 541, 557, 593, 619, 673, 683, 733, 743, 839, 881, 929, 953</pre>
=={{header|Nim}}==
Line 372 ⟶ 444:
template isPrime(n: int): bool = not composite[n]
let primes = collect
for n in 2..N:
if n.isPrime: n
Line 414 ⟶ 486:
21 found: {2,3,7,13,37,43,281,311,503,541,557,593,619,673,683,733,743,839,881,929,953}
</pre>
=={{header|Prolog}}==
runs with swi-prolog
<syntaxhighlight lang="prolog">
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|Quackery}}==
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
<syntaxhighlight lang="Quackery"> 0 1000 times [ i^ isprime if [ i^ + dup isprime if [ i^ echo sp ] drop ] ]</syntaxhighlight>
{{out}}
<pre>2 3 7 13 37 43 281 311 503 541 557 593 619 673 683 733 743 839 881 929 953</pre>
=={{header|Raku}}==
Line 602 ⟶ 729:
Found 21 numbers
done...
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
« { } 0 0
'''WHILE''' DUP 1000 < '''REPEAT'''
NEXTPRIME SWAP OVER + SWAP
'''IF''' OVER ISPRIME? '''THEN''' ROT OVER + UNROT '''END'''
'''END''' DROP2
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { 2 3 7 13 37 43 281 311 503 541 557 593 619 673 683 733 743 839 881 929 953 }
</pre>
Line 639 ⟶ 779:
prime: 929 prime sum: 66463
prime: 953 prime sum: 70241
</pre>
=={{header|Uiua}}==
{{works with|Uiua|0.10.0-dev.1}}
<syntaxhighlight lang="Uiua">
# Build primes by sieve. Limit found by inspection.
⇌◌⍢(▽≠0◿⊃⊢(.↘1)⟜(⊂⊢)|>0⧻) ↘2⇡80000 []
# Build running sums.
\+▽<1000...
# # Find sums that are prime, then prettify.
⧻.⍉⊟:∩(⬚0▽),⟜∊
</syntaxhighlight>
{{out}}
<pre>
╭─
╷ 2 2
3 5
7 17
13 41
37 197
43 281
281 7699
311 8893
503 22039
541 24133
557 25237
593 28697
619 32353
673 37561
683 38921
733 43201
743 44683
839 55837
881 61027
929 66463
953 70241
╯
21
</pre>
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
import "./
var primes = Int.primeSieve(1000, true)
Line 659 ⟶ 836:
}
System.print("Primes 'p' under 1000 where the sum of all primes <= p is also prime:")
Fmt.tprint("$4d", results, 7)
System.print("\nFound %(results.count) such primes.")</syntaxhighlight>
Line 698 ⟶ 875:
");
]</syntaxhighlight>
{{out}}
<pre>2 3 7 13 37 43 281 311 503 541
557 593 619 673 683 733 743 839 881 929
953
|