How to resolve the algorithm Earliest difference between prime gaps step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Earliest difference between prime gaps step by step in the J 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 J programming language

Source code in the j programming language

lowpgap=: {{
  magnitude=. ref=. 10^y
  whilst. -.1 e. ok do.
    magnitude=. 10*magnitude
    g=. 2-~/\ p=.p: i.magnitude
    mag=. p{~}.g i. i.&.-: >./g NB. minimum adjacent gaps
    dif=. 2-~/\ mag
    ok=. ref < dif
  end.
  (0 1+ok i.1){mag
}}


   lowpgap 1
7 23
   lowpgap 2
113 1831
   lowpgap 3
113 1831
   lowpgap 4
9551 30593
   lowpgap 5
31397 404597
   lowpgap 6
396733 1444309
   lowpgap 7
2010733 13626257


  

You may also check:How to resolve the algorithm SHA-256 step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Factorial step by step in the REXX programming language
You may also check:How to resolve the algorithm Echo server step by step in the Factor programming language
You may also check:How to resolve the algorithm Take notes on the command line step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the Batch File programming language