Summarize primes: Difference between revisions

Content added Content deleted
(Added Forth solution)
(added AWK)
Line 5: Line 5:
<br><br>
<br><br>


=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f SUMMARIZE_PRIMES.AWK
BEGIN {
start = 1
stop = 999
for (i=start; i<=stop; i++) {
if (is_prime(i)) {
count1++
sum += i
if (is_prime(sum)) {
printf("the sum of %3d primes from primes 2-%-3s is %5d which is also prime\n",count1,i,sum)
count2++
}
}
}
printf("Summarized primes %d-%d: %d\n",start,stop,count2)
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
</lang>
{{out}}
<pre>
the sum of 1 primes from primes 2-2 is 2 which is also prime
the sum of 2 primes from primes 2-3 is 5 which is also prime
the sum of 4 primes from primes 2-7 is 17 which is also prime
the sum of 6 primes from primes 2-13 is 41 which is also prime
the sum of 12 primes from primes 2-37 is 197 which is also prime
the sum of 14 primes from primes 2-43 is 281 which is also prime
the sum of 60 primes from primes 2-281 is 7699 which is also prime
the sum of 64 primes from primes 2-311 is 8893 which is also prime
the sum of 96 primes from primes 2-503 is 22039 which is also prime
the sum of 100 primes from primes 2-541 is 24133 which is also prime
the sum of 102 primes from primes 2-557 is 25237 which is also prime
the sum of 108 primes from primes 2-593 is 28697 which is also prime
the sum of 114 primes from primes 2-619 is 32353 which is also prime
the sum of 122 primes from primes 2-673 is 37561 which is also prime
the sum of 124 primes from primes 2-683 is 38921 which is also prime
the sum of 130 primes from primes 2-733 is 43201 which is also prime
the sum of 132 primes from primes 2-743 is 44683 which is also prime
the sum of 146 primes from primes 2-839 is 55837 which is also prime
the sum of 152 primes from primes 2-881 is 61027 which is also prime
the sum of 158 primes from primes 2-929 is 66463 which is also prime
the sum of 162 primes from primes 2-953 is 70241 which is also prime
Summarized primes 1-999: 21
</pre>
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
Line 36: Line 92:
Real: 00:00:00.015
Real: 00:00:00.015
</pre>
</pre>

=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
{{works with|Factor|0.99 2021-02-05}}