Summarize primes
Summarize first n primes (p) and check if it is a prime, where p < 1000
Summarize primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
Factor
<lang factor>USING: assocs formatting kernel math.primes math.ranges math.statistics prettyprint ;
1000 [ [1,b] ] [ primes-upto cum-sum ] bi zip [ nip prime? ] assoc-filter [ "The sum of the first %3d primes is %5d (which is prime).\n" printf ] assoc-each</lang>
- Output:
The sum of the first 1 primes is 2 (which is prime). The sum of the first 2 primes is 5 (which is prime). The sum of the first 4 primes is 17 (which is prime). The sum of the first 6 primes is 41 (which is prime). The sum of the first 12 primes is 197 (which is prime). The sum of the first 14 primes is 281 (which is prime). The sum of the first 60 primes is 7699 (which is prime). The sum of the first 64 primes is 8893 (which is prime). The sum of the first 96 primes is 22039 (which is prime). The sum of the first 100 primes is 24133 (which is prime). The sum of the first 102 primes is 25237 (which is prime). The sum of the first 108 primes is 28697 (which is prime). The sum of the first 114 primes is 32353 (which is prime). The sum of the first 122 primes is 37561 (which is prime). The sum of the first 124 primes is 38921 (which is prime). The sum of the first 130 primes is 43201 (which is prime). The sum of the first 132 primes is 44683 (which is prime). The sum of the first 146 primes is 55837 (which is prime). The sum of the first 152 primes is 61027 (which is prime). The sum of the first 158 primes is 66463 (which is prime). The sum of the first 162 primes is 70241 (which is prime).
Ring
<lang ring> load "stdlib.ring" see "working..." + nl see "Summarize primes:" + nl see "n sum" + nl row = 0 sum = 0 limit = 1000 Primes = []
for n = 2 to limit
if isprime(n) add(Primes,n) ok
next
for n = 1 to len(Primes)
sum = sum + Primes[n] if isprime(sum) row = row + 1 see "" + n + " " + sum + nl ok
next
see "Found " + row + " numbers" + nl see "done..." + nl </lang>
- Output:
working... Summarize primes: n sum 1 2 2 5 4 17 6 41 12 197 14 281 60 7699 64 8893 96 22039 100 24133 102 25237 108 28697 114 32353 122 37561 124 38921 130 43201 132 44683 146 55837 152 61027 158 66463 162 70241 Found 21 numbers done...