Equal prime and composite sums: Difference between revisions

Content added Content deleted
m (typo)
Line 54: Line 54:
Primes up to 390180569 at position 20840220 and composites up to 91491160 at position 86192660 sum to 3950430820867201.
Primes up to 390180569 at position 20840220 and composites up to 91491160 at position 86192660 sum to 3950430820867201.
</pre>
</pre>
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''

See [[Erdős-primes#jq]] for a suitable definition of `is_prime` as used here.

The program given in this entry requires foreknowledge of the appropriate size of the Eratosthenes sieve.
<lang jq>def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] +.;

def primeSieve: [range(0;.) | if is_prime then . else false end];

def task($sievesize):
($sievesize | primeSieve) as $p
| {compSums:[0],
primeSums:[],
csum:0,
psum:0 }
| reduce range(2; $sievesize) as $i (.;
if $p[$i] == false
then .csum += $i
| .compSums += [ .csum ]
else
.psum += $i
| .primeSums += [.psum]
end)
| range(0; .primeSums|length) as $i
| .primeSums[$i] as $ps
| (.compSums | index( $ps )) as $ix
| select($ix >= 0)
| "\($ps|lpad(21)) - \($i+1|lpad(21)) prime sum, \($ix|lpad(12)) composite sum"
;

task(1E5)</lang>
{{out}}
<pre>
10 - 3 prime sum, 2 composite sum
1988 - 33 prime sum, 51 composite sum
14697 - 80 prime sum, 147 composite sum
83292 - 175 prime sum, 361 composite sum
1503397 - 660 prime sum, 1582 composite sum
18859052 - 2143 prime sum, 5699 composite sum
93952013 - 4556 prime sum, 12821 composite sum
</pre>

=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<lang julia>using Primes