Equal prime and composite sums: Difference between revisions

Realize in F#
m (→‎{{header|Perl}}: wrong limit)
(Realize in F#)
Line 32:
 
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
// Equal prime and composite sums. Nigel Galloway: March 3rd., 2022
let fN(g:seq<int64>)=let g=(g|>Seq.scan(fun(_,n,i) g->(g,n+g,i+1))(0,0L,0)|>Seq.skip 1).GetEnumerator() in (fun()->g.MoveNext()|>ignore; g.Current)
let fG n g=let rec fG a b=seq{match a,b with ((_,p,_),(_,c,_)) when p<c->yield! fG(n()) b |((_,p,_),(_,c,_)) when p>c->yield! fG a (g()) |_->yield(a,b); yield! fG(n())(g())} in fG(n())(g())
fG(fN(primes64()))(fN(primes64()|>Seq.pairwise|>Seq.collect(fun(n,g)->[1L+n..g-1L])))|>Seq.take 11|>Seq.iter(fun((n,i,g),(e,_,l))->printfn $"Primes up to %d{n} at position %d{g} and composites up to %d{e} at position %d{l} sum to %d{i}.")
</lang>
{{out}}
<pre>
Primes up to 5 at position 3 and composites up to 6 at position 2 sum to 10.
Primes up to 137 at position 33 and composites up to 72 at position 51 sum to 1988.
Primes up to 409 at position 80 and composites up to 190 at position 147 sum to 14697.
Primes up to 1039 at position 175 and composites up to 448 at position 361 sum to 83292.
Primes up to 4937 at position 660 and composites up to 1868 at position 1582 sum to 1503397.
Primes up to 18787 at position 2143 and composites up to 6544 at position 5699 sum to 18859052.
Primes up to 43753 at position 4556 and composites up to 14522 at position 12821 sum to 93952013.
Primes up to 1565929 at position 118785 and composites up to 440305 at position 403341 sum to 89171409882.
Primes up to 17662763 at position 1131142 and composites up to 4548502 at position 4229425 sum to 9646383703961.
Primes up to 86254457 at position 5012372 and composites up to 21123471 at position 19786181 sum to 209456854921713.
Primes up to 390180569 at position 20840220 and composites up to 91491160 at position 86192660 sum to 3950430820867201.
</pre>
=={{header|Julia}}==
<lang julia>using Primes
Line 51 ⟶ 73:
incrementpsum()
incrementcsum()
endendrd
end
while csum < psum
2,172

edits