Summarize primes: Difference between revisions

(Added Fōrmulæ solution)
Line 705:
(158,929,66463)
(162,953,70241)</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<lang jq>def is_prime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
elif ($n % 5 == 0) then $n == 5
elif ($n % 7 == 0) then $n == 7
elif ($n % 11 == 0) then $n == 11
elif ($n % 13 == 0) then $n == 13
elif ($n % 17 == 0) then $n == 17
elif ($n % 19 == 0) then $n == 19
else {i:23}
| until( (.i * .i) > $n or ($n % .i == 0); .i += 2)
| .i * .i > $n
end;
 
# primes up to but excluding $n
def primes($n): [range(2;$n) | select(is_prime)];
 
"Prime sums of primes less than 1000",
(primes(1000)
| . as $p1000
| range(1; length)
| ($p1000[: .] | add) as $sum
| select($sum | is_prime)
| "The sum of the \(.) primes from 2 to \($p1000[.-1]) is \($sum)." )
</lang>
{{out}}
<pre>
Prime sums of primes less than 1000
The sum of the 1 primes from 2 to 2 is 2.
The sum of the 2 primes from 2 to 3 is 5.
The sum of the 4 primes from 2 to 7 is 17.
The sum of the 6 primes from 2 to 13 is 41.
The sum of the 12 primes from 2 to 37 is 197.
The sum of the 14 primes from 2 to 43 is 281.
The sum of the 60 primes from 2 to 281 is 7699.
The sum of the 64 primes from 2 to 311 is 8893.
The sum of the 96 primes from 2 to 503 is 22039.
The sum of the 100 primes from 2 to 541 is 24133.
The sum of the 102 primes from 2 to 557 is 25237.
The sum of the 108 primes from 2 to 593 is 28697.
The sum of the 114 primes from 2 to 619 is 32353.
The sum of the 122 primes from 2 to 673 is 37561.
The sum of the 124 primes from 2 to 683 is 38921.
The sum of the 130 primes from 2 to 733 is 43201.
The sum of the 132 primes from 2 to 743 is 44683.
The sum of the 146 primes from 2 to 839 is 55837.
The sum of the 152 primes from 2 to 881 is 61027.
The sum of the 158 primes from 2 to 929 is 66463.
The sum of the 162 primes from 2 to 953 is 70241.
</pre>
 
=={{header|Julia}}==
2,472

edits