Summarize primes: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) m →{{header|Raku}}: fix off-by-one error |
Added Wren |
||
Line 178: | Line 178: | ||
Found 21 numbers |
Found 21 numbers |
||
done... |
done... |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{libheader|Wren-math}} |
|||
{{libheader|Wren-fmt}} |
|||
<lang ecmascript>import "/math" for Int |
|||
import "/fmt" for Fmt |
|||
var primes = Int.primeSieve(999) |
|||
var sum = 0 |
|||
var n = 0 |
|||
var c = 0 |
|||
System.print("Summing the first n primes (<1,000) where the sum is itself prime:") |
|||
System.print(" n cumulative sum") |
|||
for (p in primes) { |
|||
n = n + 1 |
|||
sum = sum + p |
|||
if (Int.isPrime(sum)) { |
|||
c = c + 1 |
|||
Fmt.print("$3d $,6d", n, sum) |
|||
} |
|||
} |
|||
System.print("\n%(c) such prime sums found")</lang> |
|||
{{out}} |
|||
<pre> |
|||
Summing the first n primes (<1,000) where the sum is itself prime: |
|||
n cumulative sum |
|||
1 2 |
|||
2 5 |
|||
4 17 |
|||
6 41 |
|||
12 197 |
|||
14 281 |
|||
60 7,699 |
|||
64 8,893 |
|||
96 22,039 |
|||
100 24,133 |
|||
102 25,237 |
|||
108 28,697 |
|||
114 32,353 |
|||
122 37,561 |
|||
124 38,921 |
|||
130 43,201 |
|||
132 44,683 |
|||
146 55,837 |
|||
152 61,027 |
|||
158 66,463 |
|||
162 70,241 |
|||
21 such prime sums found |
|||
</pre> |
</pre> |