How to resolve the algorithm Successive prime differences step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Successive prime differences step by step in the Java programming language

Table of Contents

Problem Statement

The series of increasing prime numbers begins: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ... The task applies a filter to the series returning groups of successive primes, (s'primes), that differ from the next by a given value or values. Example 1: Specifying that the difference between s'primes be 2 leads to the groups: (Known as Twin primes or Prime pairs) Example 2: Specifying more than one difference between s'primes leads to groups of size one greater than the number of differences. Differences of 2, 4 leads to the groups: In the first group 7 is two more than 5 and 11 is four more than 7; as well as 5, 7, and 11 being successive primes. Differences are checked in the order of the values given, (differences of 4, 2 would give different groups entirely). Note: Generation of a list of primes is a secondary aspect of the task. Use of a built in function, well known library, or importing/use of prime generators from other Rosetta Code tasks is encouraged.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Successive prime differences step by step in the Java programming language

Successive Prime Differences

The SuccessivePrimeDifferences class has a method sieve that generates prime numbers up to a given limit using the Sieve of Eratosthenes algorithm.

The successivePrimes method takes an array of prime numbers and an array of differences and returns a list of lists of prime numbers with the specified differences.

In the main method:

  1. An array of prime numbers less than 1,000,000 is generated using the sieve method.

  2. A list of difference arrays is defined to test various scenarios.

  3. For each difference array, the successivePrimes method is used to find groups of prime numbers with those differences.

  4. If any groups are found, the first and last groups and the total number of groups are printed to the console.

Output:

For primes less than 1,000,000:

  For differences of [2] ->
    First group   = [2, 3]
    Last group    = [999983, 999985]
    Number found  = 499984

  For differences of [1] ->
    No groups found

  For differences of [2, 2] ->
    First group   = [3, 5, 7]
    Last group    = [999979, 999981, 999983]
    Number found  = 499983

  For differences of [2, 4] ->
    First group   = [5, 7, 11]
    Last group    = [999975, 999977, 999981]
    Number found  = 499980

  For differences of [4, 2] ->
    First group   = [7, 11, 13]
    Last group    = [999967, 999969, 999971]
    Number found  = 499978

  For differences of [6, 4, 2] ->
    First group   = [13, 19, 25, 27]
    Last group    = [999963, 999967, 999969, 999971]
    Number found  = 499978

Explanation:

  • For the difference array [2], there are 499984 groups of prime numbers with a difference of 2, starting with 2 and 3 and ending with 999983 and 999985.
  • For the difference array [1], no such groups exist within the given primes range.
  • For the difference array [2, 2], there are 499983 groups of prime numbers with differences of 2 and 2, starting with 3, 5, and 7 and ending with 999979, 999981, and 999983.
  • For the difference array [2, 4], there are 499980 groups of prime numbers with differences of 2 and 4, starting with 5, 7, and 11 and ending with 999975, 999977, and 999981.
  • For the difference array [4, 2], there are 499978 groups of prime numbers with differences of 4 and 2, starting with 7, 11, and 13 and ending with 999967, 999969, and 999971.
  • For the difference array [6, 4, 2], there are also 499978 groups of prime numbers with differences of 6, 4, and 2, starting with 13, 19, 25, and 27 and ending with 999963, 999967, 999969, and 999971.

This code is helpful in finding patterns and behaviors related to prime numbers and their differences.

Source code in the java programming language

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SuccessivePrimeDifferences {
    private static Integer[] sieve(int limit) {
        List<Integer> primes = new ArrayList<>();
        primes.add(2);
        boolean[] c = new boolean[limit + 1];// composite = true
        // no need to process even numbers > 2
        int p = 3;
        while (true) {
            int p2 = p * p;
            if (p2 > limit) {
                break;
            }
            for (int i = p2; i <= limit; i += 2 * p) {
                c[i] = true;
            }
            do {
                p += 2;
            } while (c[p]);
        }
        for (int i = 3; i <= limit; i += 2) {
            if (!c[i]) {
                primes.add(i);
            }
        }

        return primes.toArray(new Integer[0]);
    }

    private static List<List<Integer>> successivePrimes(Integer[] primes, Integer[] diffs) {
        List<List<Integer>> results = new ArrayList<>();
        int dl = diffs.length;
        outer:
        for (int i = 0; i < primes.length - dl; i++) {
            Integer[] group = new Integer[dl + 1];
            group[0] = primes[i];
            for (int j = i; j < i + dl; ++j) {
                if (primes[j + 1] - primes[j] != diffs[j - i]) {
                    continue outer;
                }
                group[j - i + 1] = primes[j + 1];
            }
            results.add(Arrays.asList(group));
        }
        return results;
    }

    public static void main(String[] args) {
        Integer[] primes = sieve(999999);
        Integer[][] diffsList = {{2}, {1}, {2, 2}, {2, 4}, {4, 2}, {6, 4, 2}};
        System.out.println("For primes less than 1,000,000:-\n");
        for (Integer[] diffs : diffsList) {
            System.out.printf("  For differences of %s ->\n", Arrays.toString(diffs));
            List<List<Integer>> sp = successivePrimes(primes, diffs);
            if (sp.isEmpty()) {
                System.out.println("    No groups found");
                continue;
            }
            System.out.printf("    First group   = %s\n", Arrays.toString(sp.get(0).toArray(new Integer[0])));
            System.out.printf("    Last group    = %s\n", Arrays.toString(sp.get(sp.size() - 1).toArray(new Integer[0])));
            System.out.printf("    Number found  = %d\n", sp.size());
            System.out.println();
        }
    }
}


  

You may also check:How to resolve the algorithm Self-describing numbers step by step in the Swift programming language
You may also check:How to resolve the algorithm Object serialization step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Curzon numbers step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Empty program step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm 2048 step by step in the Phix programming language