Summarize primes: Difference between revisions
New post.
Alextretyak (talk | contribs) (Added 11l) |
(New post.) |
||
(18 intermediate revisions by 10 users not shown) | |||
Line 12:
{{trans|Nim}}
<
I a == 2
R 1B
Line 30:
s += n
I is_prime(s)
print(f:‘{idx:3} {n:5} {s:7}’)</
{{out}}
Line 60:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<
# sieve the primes to 999 #
PR read "primes.incl.a68" PR
Line 102:
)
)
END</
{{out}}
<pre>
Line 133:
=={{header|ALGOL W}}==
<
integer MAX_NUMBER;
MAX_NUMBER := 999;
Line 188:
)
end
end.</
{{out}}
<pre>
Line 220:
=={{header|Arturo}}==
<
(pad "prime" 6) ++ " | " ++
(pad "prime sum" 11)
Line 236:
(pad to :string s 11)
]
]</
{{out}}
Line 265:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SUMMARIZE_PRIMES.AWK
BEGIN {
Line 294:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 320:
Summarized primes 1-999: 21
</pre>
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">#include "isprime.kbs"
print 1, 2, 2
sum = 2
n = 1
for i = 3 to 999 step 2
if isPrime(i) then
sum += i
n += 1
if isPrime(sum) then
print n, i, sum
end if
end if
next i</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">#include "isprime.bas"
print 1,2,2
dim as integer sum = 2, i, n=1
for i = 3 to 999 step 2
if isprime(i) then
sum += i
n+=1
if isprime(sum) then
print n, i, sum
end if
end if
next i</syntaxhighlight>
{{out}}
<pre>
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</pre>
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Use "isprime.bas"
Public Sub Main()
Print 1, 2, 2
Dim n As Integer = 1, i As Integer, sum As Integer = 2
For i = 3 To 999 Step 2
If isPrime(i) Then
sum += i
n += 1
If isPrime(sum) Then
Print n, i, sum
End If
End If
Next
End</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|PureBasic}}===
<syntaxhighlight lang="vb">;XIncludeFile "isprime.pb"
OpenConsole()
Define.i sum, i, n
PrintN("1" + #TAB$ + "2" + #TAB$ + "2")
sum = 2
n = 1
For i = 3 To 999 Step 2
If isPrime(i):
sum + i
n + 1
If isPrime(sum):
PrintN(Str(n) + #TAB$ + Str(i) + #TAB$ + Str(sum))
EndIf
EndIf
Next i
Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">//import isprime
print 1, chr$(9), 2, chr$(9), 2
sum = 2
n = 1
for i = 3 to 999 step 2
if isPrime(i) then
sum = sum + i
n = n + 1
if isPrime(sum) print n, chr$(9), i, chr$(9), sum
fi
next i
end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
=={{header|C}}==
{{trans|C++}}
<
#include <stdio.h>
Line 377 ⟶ 494:
return 0;
}</
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
Line 403 ⟶ 520:
=={{header|C++}}==
<
bool is_prime(int n) {
Line 454 ⟶ 571:
return 0;
}</
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
Line 478 ⟶ 595:
The sum of 162 primes in [2, 953] is 70241 which is also prime
There are 21 summerized primes in [1, 1000)</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Uses the [[Extensible_prime_generator#Delphi|Delphi Prime-Generator Object]]
<syntaxhighlight lang="Delphi">
procedure SumOfPrimeSequences(Memo: TMemo);
var Sieve: TPrimeSieve;
var I,Inx, Sum: integer;
begin
Sieve:=TPrimeSieve.Create;
try
Sieve.Intialize(100000);
Memo.Lines.Add(' I P(I) Sum');
Memo.Lines.Add('---------------');
I:=0;
Sum:=0;
while Sieve.Primes[I]<1000 do
begin
Sum:=Sum+Sieve.Primes[I];
if Sieve.Flags[Sum] then
begin
Memo.Lines.Add(Format('%3d %4d %6d',[I,Sieve.Primes[I],Sum]));
end;
Inc(I,1);
end;
finally Sieve.Free; end;
end;
</syntaxhighlight>
{{out}}
<pre>
I P(I) Sum
---------------
0 2 2
1 3 5
3 7 17
5 13 41
11 37 197
13 43 281
59 281 7699
63 311 8893
95 503 22039
99 541 24133
101 557 25237
107 593 28697
113 619 32353
121 673 37561
123 683 38921
129 733 43201
131 743 44683
145 839 55837
151 881 61027
157 929 66463
161 953 70241
Elapsed Time: 31.759 ms.
</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func prime n .
if n mod 2 = 0 and n > 2
return 0
.
i = 3
while i <= sqrt n
if n mod i = 0
return 0
.
i += 2
.
return 1
.
for i = 2 to 999
if prime i = 1
ind += 1
sum += i
if prime sum = 1
print ind & ": " & sum
.
.
.
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<
// Summarize Primes: Nigel Galloway. April 16th., 2021
primes32()|>Seq.takeWhile((>)1000)|>Seq.scan(fun(n,g) p->(n+1,g+p))(0,0)|>Seq.filter(snd>>isPrime)|>Seq.iter(fun(n,g)->printfn "%3d->%d" n g)
</syntaxhighlight>
{{out}}
<pre>
Line 513 ⟶ 720:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
math.statistics prettyprint ;
1000 [ [1,b] ] [ primes-upto cum-sum ] bi zip
[ nip prime? ] assoc-filter
[ "The sum of the first %3d primes is %5d (which is prime).\n" printf ] assoc-each</
{{out}}
<pre>
Line 545 ⟶ 752:
=={{header|Fermat}}==
<
s:=0
for i=1, 162 do s:=s+Prime(i);if Isprime(s)=1 then n:=n+1;!!(n,Prime(i),s) fi od
</syntaxhighlight>
{{out}}
<pre>
Line 576 ⟶ 783:
=={{header|Forth}}==
{{works with|Gforth}}
<
dup 2 < if drop false exit then
dup 2 mod 0= if 2 = exit then
Line 605 ⟶ 812:
main
bye</
{{out}}
<pre>count prime sum
1 2 2
2 3 5
Line 630 ⟶ 836:
152 881 61027
158 929 66463
162 953 70241</pre>
=={{header|
{{FormulaeEntry|page=https://formulae.org/?script=examples/Summarize_primes}}
'''Solution'''
[[File:Fōrmulæ - Summarize primes 01.png]]
[[File:Fōrmulæ - Summarize primes 02.png]]
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 704 ⟶ 873:
fmt.Println()
fmt.Println(c, "such prime sums found")
}</
{{out}}
<pre>
Line 711 ⟶ 880:
=={{header|Haskell}}==
<
import Data.Numbers.Primes (isPrime, primes)
Line 729 ⟶ 898:
mapM_ print $
takeWhile (\(_, p, _) -> 1000 > p) indexedPrimeSums
</syntaxhighlight>
{{Out}}
<pre>(1,2,2)
Line 752 ⟶ 921:
(158,929,66463)
(162,953,70241)</pre>
=={{header|J}}==
<syntaxhighlight lang="j">primes=: p: i. _1 p: 1000 NB. all prime numbers below 1000
sums=: +/\ primes NB. running sum of those primes
mask=: 1 p: sums NB. array of 0s, 1s where sums are primes
NB. indices of prime sums (incremented for 1-based indexing)
NB. "copy" only the final primes in the prime sums
NB. "copy" only the sums which are prime
results=: (>: I. mask) ,. (mask # primes) ,. (mask # sums)
NB. pretty-printed "boxed" output
output=: 2 1 $ ' n prime sum ' ; < results</syntaxhighlight>
{{out}}
<pre> output
┌─────────────┐
│ n prime sum │
├─────────────┤
│ 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│
└─────────────┘</pre>
=={{header|Java}}==
<syntaxhighlight lang="java">
public final class SummarizePrimes {
public static void main(String[] args) {
final int start = 1;
final int finish = 1_000;
int sum = 0;
int count = 0;
int summarizedCount = 0;
for ( int p = start; p < finish; p++ ) {
if ( isPrime(p) ) {
count += 1;
sum += p;
if ( isPrime(sum) ) {
String word = ( count == 1 ) ? " prime" : " primes";
System.out.println(
"The sum of " + count + word + " in [2, " + p + "] is " + sum + ", which is also prime.");
summarizedCount++;
}
}
}
System.out.println(System.lineSeparator() +
"There are " + summarizedCount + " summarized primes in [" + start + ", " + finish + "].");
}
private static boolean isPrime(int number) {
if ( number < 2 ) {
return false;
}
if ( number % 2 == 0 ) {
return number == 2;
}
if ( number % 3 == 0 ) {
return number == 3;
}
int test = 5;
while ( test * test <= number ) {
if ( number % test == 0 ) {
return false;
}
test += 2;
if ( number % test == 0 ) {
return false;
}
test += 4;
}
return true;
}
}
</syntaxhighlight>
{{ out }}
<pre>
The sum of 1 prime in [2, 2] is 2, which is also prime.
The sum of 2 primes in [2, 3] is 5, which is also prime.
The sum of 4 primes in [2, 7] is 17, which is also prime.
The sum of 6 primes in [2, 13] is 41, which is also prime.
The sum of 12 primes in [2, 37] is 197, which is also prime.
The sum of 14 primes in [2, 43] is 281, which is also prime.
The sum of 60 primes in [2, 281] is 7699, which is also prime.
The sum of 64 primes in [2, 311] is 8893, which is also prime.
The sum of 96 primes in [2, 503] is 22039, which is also prime.
The sum of 100 primes in [2, 541] is 24133, which is also prime.
The sum of 102 primes in [2, 557] is 25237, which is also prime.
The sum of 108 primes in [2, 593] is 28697, which is also prime.
The sum of 114 primes in [2, 619] is 32353, which is also prime.
The sum of 122 primes in [2, 673] is 37561, which is also prime.
The sum of 124 primes in [2, 683] is 38921, which is also prime.
The sum of 130 primes in [2, 733] is 43201, which is also prime.
The sum of 132 primes in [2, 743] is 44683, which is also prime.
The sum of 146 primes in [2, 839] is 55837, which is also prime.
The sum of 152 primes in [2, 881] is 61027, which is also prime.
The sum of 158 primes in [2, 929] is 66463, which is also prime.
The sum of 162 primes in [2, 953] is 70241, which is also prime.
There are 21 summarized primes in [1, 1000].
</pre>
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<
. as $n
| if ($n < 2) then false
Line 780 ⟶ 1,078:
| (.[: $n] | add) as $sum
| select($sum | is_prime)
| "The sum of the \($n) primes from 2 to \(.[$n-1]) is \($sum)." )</
{{out}}
<pre>
Line 808 ⟶ 1,106:
=={{header|Julia}}==
<
p1000 = primes(1000)
Line 819 ⟶ 1,117:
end
end
</
<pre>
The sum of the 1 primes from prime 2 to prime 2 is 2, which is prime.
Line 845 ⟶ 1,143:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
TableForm[
Select[Transpose[{Range[Length[p]], p, Accumulate[p]}], Last /* PrimeQ],
TableHeadings -> {None, {"Prime count", "Prime", "Prime sum"}}
]</
{{out}}
<pre>Prime count Prime Prime sum
Line 875 ⟶ 1,173:
=={{header|Nim}}==
<
const N = 999
Line 897 ⟶ 1,195:
inc idx
s += n
if s.isPrime: echo &"{idx:3} {n:5} {s:7}"</
{{out}}
Line 925 ⟶ 1,223:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use ntheory <nth_prime is_prime>;
Line 934 ⟶ 1,232:
} until $n >= $limit;
print "Of the first $limit primes: @{[scalar @sums]} cumulative prime sums:\n", join "\n", @sums;</
{{out}}
<pre style="height:70ex">Of the first 1000 primes: 76 cumulative prime sums:
Line 1,016 ⟶ 1,314:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">sp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">))),</span><span style="color: #000000;">sp</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Found %d of em: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<!--</
{{out}}
<pre>
Found 21 of em: 1, 2, 4, 6, 12, ..., 132, 146, 152, 158, 162
</pre>
=={{header|Prolog}}==
works with swi-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>
=={{header|Python}}==
<
Line 1,101 ⟶ 1,453:
if __name__ == '__main__':
main()
</syntaxhighlight>
{{Out}}
<pre>1 -> 2
Line 1,124 ⟶ 1,476:
158 -> 66463
162 -> 70241</pre>
=={{header|Quackery}}==
<code>isprime</code> is defined at [[Primality by trial division#Quackery]].
<syntaxhighlight lang="Quackery"> [ number$
space 6 of swap join
-7 split nip echo$ ] is rjust ( n --> )
say " index prime sum" cr
say " ----- ----- ---" cr
[] 1000 times
[ i^ isprime if [ i^ join ] ]
0 swap
witheach
[ dup dip +
over isprime iff
[ i^ 1+ rjust
rjust
dup rjust
cr ]
else drop ]
drop
</syntaxhighlight>
{{out}}
<pre> index prime sum
----- ----- ---
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</pre>
=={{header|Raku}}==
<syntaxhighlight lang="raku"
my @primes = grep *.is-prime, ^Inf;
Line 1,136 ⟶ 1,538:
}
).join("\n")
given grep { @primesums[$_].is-prime }, ^1000;</
{{out}}
<pre>76 cumulative prime sums:
Line 1,217 ⟶ 1,619:
=={{header|REXX}}==
<
parse arg hi . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 1000 /*Not specified? Then use the default.*/
Line 1,254 ⟶ 1,656:
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # Ps; assign next P; P square; P*/
if @.#<hi then sP= sP + @.# /*maybe add this prime to sum─of─primes*/
end /*j*/; return</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,286 ⟶ 1,688:
=={{header|Ring}}==
<
load "stdlib.ring"
see "working..." + nl
Line 1,312 ⟶ 1,714:
see "Found " + row + " numbers" + nl
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,341 ⟶ 1,743:
Found 21 numbers
done...
</pre>
=={{header|RPL}}==
{{works with|HP|49g}}
≪ { } 0 0 → display length sum
≪ 1
'''WHILE''' DUP 1000 < '''REPEAT'''
NEXTPRIME
DUP 'sum' STO+
1 'length' STO+
'''IF''' sum ISPRIME? '''THEN'''
length OVER sum 3 →LIST 1 →LIST 'display' SWAP STO+ '''END'''
'''END'''
DROP display
≫ ≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: {{1 2 2} {2 3 5} {3 7 17} {4 13 41} {5 37 197} {6 43 281} {7 281 7699} {8 311 8893} {9 503 22039} {10 541 24133} {11 557 25237} {12 593 28697} {13 619 32353} {14 673 37561} {15 683 38921} {16 733 43201} {17 743 44683} {18 839 55837} {19 881 61027} {20 929 66463} {21 953 70241}}
</pre>
=={{header|Ruby}}==
{{trans|C++}}
<
if n < 2 then
return false
Line 1,389 ⟶ 1,809:
end
end
print "There are %d summerized primes in [%d, %d]\n" % [sc, START, STOP]</
{{out}}
<pre>The sum of 1 primes in [2, 2] is 2 which is also prime
Line 1,415 ⟶ 1,835:
=={{header|Rust}}==
<
// primal = "0.3"
Line 1,432 ⟶ 1,852:
}
}
}</
{{out}}
Line 1,458 ⟶ 1,878:
158 929 66463
162 953 70241
</pre>
=={{header|Scala}}==
<syntaxhighlight lang="scala">object PrimeSum extends App {
val oddPrimes: LazyList[Int] = 3 #:: LazyList.from(5, 2)
.filter(p => oddPrimes.takeWhile(_ <= math.sqrt(p)).forall(p % _ > 0))
val primes = 2 #:: oddPrimes
def isPrime(n: Int): Boolean = {
if (n < 5) (n | 1) == 3
else primes.takeWhile(_ <= math.sqrt(n)).forall(n % _ > 0)
}
val limit = primes.takeWhile(_ <= 1000).length
val number = (1 to limit).filter(index => {
val list = primes.take(index)
val sum = list.sum
val flag = isPrime(sum)
if (flag) println(f"$index%3d ${list.last}%3d $sum%5d")
flag
}).length
println(s"\nfound $number such primes")
}</syntaxhighlight>
{{out}}
<pre> 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>
=={{header|Sidef}}==
<
[k+1, prime(k+1), v]
}.grep { .tail.is_prime }.prepend(
Line 1,467 ⟶ 1,936:
).each_2d {|n,p,s|
printf("%5s %6s %8s\n", n, p, s)
}</
{{out}}
<pre>
Line 1,497 ⟶ 1,966:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var primes = Int.primeSieve(999)
Line 1,514 ⟶ 1,983:
}
}
System.print("\n%(c) such prime sums found")</
{{out}}
Line 1,546 ⟶ 2,015:
=={{header|XPL0}}==
<
int N, I;
[if N <= 1 then return false;
Line 1,574 ⟶ 2,043:
Text(0, " prime sums of primes found below 1000.
");
]</
{{out}}
|