Jump to content

Summarize primes: Difference between revisions

→‎{{header|ALGOL 68}}: Use ALGOL 68-primes
(→‎{{header|ALGOL 68}}: Use ALGOL 68-primes)
Line 10:
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # sum the primes below n and report the sums that are prime #
# sieve the primes to 999 #
INT max prime = 999; # largest prime to consider #
#PR sieve theread "primes.incl.a68" to max prime #PR
[]BOOL 1prime := maxPRIMESIEVE prime ]BOOL prime999;
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;
Line 26 ⟶ 21:
print( ( "prime prime", newline ) );
print( ( "count prime sum", newline ) );
FOR i TO maxUPB prime DO
IF prime[ i ] THEN
# have another prime #
Line 54 ⟶ 49:
, whole( prime sum count, 0 )
, " prime sums of primes below "
, whole( maxUPB prime + 1, 0 )
, newline
)
)
END</lang>
</lang>
{{out}}
<pre>
3,044

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.