Sum of primes in odd positions is prime: Difference between revisions

Added Algol 68
m (→‎{{header|Raku}}: oops, change subscript)
(Added Algol 68)
Line 8:
 
<br><br>
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find primes (up to 999) p(i) for odd i such that the sum of primes p(j), j = 1, 3, 5, ..., i is prime #
INT max prime = 999;
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE 50 000; # guess that the max sum will be <= 50 000 #
# count the primes up to max prime #
INT p count := 0; FOR i TO max prime DO IF prime[ i ] THEN p count +:= 1 FI OD;
# construct a list of the primes up to max prime #
[ 1 : p count ]INT low prime;
INT low pos := 0;
FOR i TO max prime DO IF prime[ i ] THEN low prime[ low pos +:= 1 ] := i FI OD;
# find the sums of the odd primes and test for primality #
print( ( " i p[i] sum", newline ) );
INT odd prime sum := 0;
FOR i BY 2 TO UPB low prime DO
IF odd prime sum +:= low prime[ i ];
IF odd prime sum <= UPB prime
THEN
prime[ odd prime sum ]
ELSE
print( ( "Need more primes: ", whole( odd prime sum, 0 ), newline ) );
FALSE
FI
THEN
print( ( whole( i, -3 ), " ", whole( low prime[ i ], -4 ), " ", whole( odd prime sum, -6 ), newline ) )
FI
OD
END</lang>
{{out}}
<pre>
i p[i] sum
1 2 2
3 5 7
11 31 89
27 103 659
35 149 1181
67 331 5021
91 467 9923
95 499 10909
99 523 11941
119 653 17959
143 823 26879
</pre>
 
=={{header|Factor}}==
3,044

edits