How to resolve the algorithm Earliest difference between prime gaps step by step in the F# programming language
How to resolve the algorithm Earliest difference between prime gaps step by step in the F# programming language
Table of Contents
Problem Statement
When calculating prime numbers > 2, the difference between adjacent primes is always an even number. This difference, also referred to as the gap, varies in an random pattern; at least, no pattern has ever been discovered, and it is strongly conjectured that no pattern exists. However, it is also conjectured that between some two adjacent primes will be a gap corresponding to every positive even integer.
This task involves locating the minimal primes corresponding to those gaps. Though every gap value exists, they don't seem to come in any particular order. For example, this table shows the gaps and minimum starting value primes for 2 through 30:
For the purposes of this task, considering only primes greater than 2, consider prime gaps that differ by exactly two to be adjacent.
For each order of magnitude m from 10¹ through 10⁶:
For an m of 10¹; The start value of gap 2 is 3, the start value of gap 4 is 7, the difference is 7 - 3 or 4. 4 < 10¹ so keep going. The start value of gap 4 is 7, the start value of gap 6 is 23, the difference is 23 - 7, or 16. 16 > 10¹ so this the earliest adjacent gap difference > 10¹.
Note: the earliest value found for each order of magnitude may not be unique, in fact, is not unique; also, with the gaps in ascending order, the minimal starting values are not strictly ascending.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Earliest difference between prime gaps step by step in the F# programming language
Source code in the fsharp programming language
// Earliest difference between prime gaps. Nigel Galloway: December 1st., 2021
let fN y=let i=System.Collections.Generic.SortedDictionary<int64,int64>()
let fN()=i|>Seq.pairwise|>Seq.takeWhile(fun(n,g)->g.Key=n.Key+2L)|>Seq.tryFind(fun(n,g)->abs(n.Value-g.Value)>y)
(fun(n,g)->let e=g-n in match i.TryGetValue(e) with (false,_)->i.Add(e,n); fN() |_->None)
[1..9]|>List.iter(fun g->let fN=fN(pown 10 g) in let n,i=(primes64()|>Seq.skip 1|>Seq.pairwise|>Seq.map fN|>Seq.find Option.isSome).Value
printfn $"%10d{pown 10 g} -> distance between start of gap %d{n.Key}=%d{n.Value} and start of gap %d{i.Key}=%d{i.Value} is %d{abs((n.Value)-(i.Value))}")
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the langur programming language
You may also check:How to resolve the algorithm Damm algorithm step by step in the Scala programming language
You may also check:How to resolve the algorithm Named parameters step by step in the Erlang programming language
You may also check:How to resolve the algorithm Zumkeller numbers step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Convex hull step by step in the ObjectIcon programming language