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

Content deleted Content added
Added Algol 68
→‎{{header|ALGOL 68}}: Use the new prime list routines in Algol 68-primes
Line 12: Line 12:
{{libheader|ALGOL 68-primes}}
{{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 #
<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
PR read "primes.incl.a68" PR
INT max prime = 999;
[]BOOL prime = PRIMESIEVE 50 000; # guess that the max sum will be <= 50 000 #
[]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;
[]INT low prime = EXTRACTPRIMESUPTO max prime FROMPRIMESIEVE prime; # get a list of primes up to max prime #
# 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 #
# find the sums of the odd primes and test for primality #
print( ( " i p[i] sum", newline ) );
print( ( " i p[i] sum", newline ) );