How to resolve the algorithm Equal prime and composite sums step by step in the F# programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Equal prime and composite sums step by step in the F# programming language
Table of Contents
Problem Statement
Suppose we have a sequence of prime sums, where each term Pn is the sum of the first n primes.
Further; suppose we have a sequence of composite sums, where each term Cm is the sum of the first m composites.
Notice that the third term of P; P3 (10) is equal to the second term of C; C2 (10);
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Equal prime and composite sums step by step in the F# programming language
Source code in the fsharp programming language
// 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}.")
You may also check:How to resolve the algorithm String interpolation (included) step by step in the Clojure programming language
You may also check:How to resolve the algorithm Blum integer step by step in the BASIC programming language
You may also check:How to resolve the algorithm Mutex step by step in the J programming language
You may also check:How to resolve the algorithm Almost prime step by step in the Rust programming language
You may also check:How to resolve the algorithm Brazilian numbers step by step in the Lua programming language