Summarize primes: Difference between revisions

Undo revision 335584 by Tigerofdarkness (talk)
(Added Algol 68)
(Undo revision 335584 by Tigerofdarkness (talk))
Line 4:
Summarize first n primes (p) and check if it is a prime, where '''p < 1000'''
<br><br>
 
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # sum the primes below n and report the sums that are prime #
INT max prime = 999; # largest prime to consider #
# sieve the primes to max prime #
[ 1 : max prime ]BOOL prime;
prime[ 1 ] := FALSE; prime[ 2 ] := TRUE;
FOR i FROM 3 BY 2 TO UPB prime DO prime[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO UPB prime DO prime[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( max prime ) DO
IF prime[ i ] THEN FOR s FROM i * i BY i + i TO UPB prime DO prime[ s ] := FALSE OD FI
OD;
# sum the primes and test the sum #
INT prime sum := 0;
INT prime count := 0;
INT prime sum count := 0;
print( ( "prime prime", newline ) );
print( ( "count sum", newline ) );
FOR i TO max prime DO
IF prime[ i ] THEN
# have another prime #
prime count +:= 1;
prime sum +:= i;
# check whether the prime sum is prime or not #
BOOL is prime := TRUE;
FOR p TO ENTIER sqrt( i ) WHILE is prime DO
IF prime[ p ] THEN is prime := prime sum MOD p /= 0 FI
OD;
IF is prime THEN
# the prime sum is also prime #
prime sum count +:= 1;
print( ( whole( prime count, -5 )
, " "
, whole( prime sum, -6 )
, newline
)
)
FI
FI
OD;
print( ( newline
, "Found "
, whole( prime sum count, 0 )
, " prime sums of primes below "
, whole( max prime + 1, 0 )
, newline
)
)
END</lang>
{{out}}
<pre>
prime prime
count sum
1 2
2 5
4 17
6 41
8 77
12 197
14 281
38 2747
48 4661
58 7141
60 7699
64 8893
66 9523
94 21037
96 22039
100 24133
102 25237
108 28697
112 31117
114 32353
122 37561
124 38921
130 43201
132 44683
140 50887
142 52519
146 55837
152 61027
158 66463
162 70241
164 72179
168 76127
 
Found 32 prime sums of primes below 1000
</pre>
 
=={{header|Arturo}}==
3,026

edits