Prime numbers p for which the sum of primes less than or equal to p is prime: Difference between revisions
Content added Content deleted
(→{{header|Phix}}: added duplicate link) |
(Added Algol 68 sample (same as the modified Sumarie primes sample)) |
||
Line 4: | Line 4: | ||
Find primes '''p''' which the sum of primes less or equal to '''p''' is prime, where '''p < 1,000'''. |
Find primes '''p''' which the sum of primes less or equal to '''p''' is prime, where '''p < 1,000'''. |
||
<br><br> |
<br><br> |
||
=={{header|ALGOL 68}}== |
|||
Same as the [[Summarize primes#ALGOL_68]] solution. |
|||
<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 prime 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 i OVER 2 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( i, -6 ) |
|||
, " " |
|||
, 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 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 |
|||
Found 21 prime sums of primes below 1000 |
|||
</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |