How to resolve the algorithm Earliest difference between prime gaps step by step in the Go programming language
How to resolve the algorithm Earliest difference between prime gaps step by step in the Go 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 Go programming language
Overview
This program finds the earliest pair of prime gaps with a difference greater than a specified limit, starting from the beginning of the prime sequence.
Detailed Explanation
-
Imports: The program imports the
fmt
package for input and output and thercu
package for prime generation and number formatting. -
Constants and Variables:
limit
: A constant representing the maximum limit for the prime search space (1e9
).gapStarts
: A map that stores the starting primes for each prime gap.primes
: A slice containing the firstlimit * 5
prime numbers.
-
Prime Generation: The
rcu.Primes
function is used to generate the firstlimit * 5
prime numbers and store them in theprimes
slice. This provides a sufficient range of primes for the search. -
Prime Gap Analysis:
- The program iterates through the prime numbers, calculating the gap between each consecutive pair using
primes[i] - primes[i-1]
. - If a gap has not been seen before, its starting prime is added to the
gapStarts
map.
- The program iterates through the prime numbers, calculating the gap between each consecutive pair using
-
Main Loop: The main loop continues indefinitely, searching for prime gaps with a difference greater than the current limit (
pm
).- It starts with a gap of 2 and searches for the earliest gap that starts at a prime larger than the current starting prime.
- When such a gap is found, the program calculates the difference between its starting prime and the starting prime of the previous gap.
- If the difference exceeds the limit, the program prints the details of the gap and updates the limit to 10 times its previous value.
- If the difference is less than or equal to the limit, the program moves on to the next gap.
Output: The program outputs the details of the earliest prime gaps with a difference greater than the specified limit, including the gap size, starting primes, and difference. It continues searching for gaps with increasingly larger differences until reaching the limit of 1e9
.
Source code in the go programming language
package main
import (
"fmt"
"rcu"
)
func main() {
limit := int(1e9)
gapStarts := make(map[int]int)
primes := rcu.Primes(limit * 5)
for i := 1; i < len(primes); i++ {
gap := primes[i] - primes[i-1]
if _, ok := gapStarts[gap]; !ok {
gapStarts[gap] = primes[i-1]
}
}
pm := 10
gap1 := 2
for {
for _, ok := gapStarts[gap1]; !ok; {
gap1 += 2
}
start1 := gapStarts[gap1]
gap2 := gap1 + 2
if _, ok := gapStarts[gap2]; !ok {
gap1 = gap2 + 2
continue
}
start2 := gapStarts[gap2]
diff := start2 - start1
if diff < 0 {
diff = -diff
}
if diff > pm {
cpm := rcu.Commatize(pm)
cst1 := rcu.Commatize(start1)
cst2 := rcu.Commatize(start2)
cd := rcu.Commatize(diff)
fmt.Printf("Earliest difference > %s between adjacent prime gap starting primes:\n", cpm)
fmt.Printf("Gap %d starts at %s, gap %d starts at %s, difference is %s.\n\n", gap1, cst1, gap2, cst2, cd)
if pm == limit {
break
}
pm *= 10
} else {
gap1 = gap2
}
}
}
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Babel programming language
You may also check:How to resolve the algorithm JortSort step by step in the Objeck programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the Picat programming language
You may also check:How to resolve the algorithm Factorial step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the Prolog programming language