How to resolve the algorithm Prime conspiracy step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Prime conspiracy step by step in the Java programming language

Table of Contents

Problem Statement

A recent discovery, quoted from   Quantamagazine   (March 13, 2016): and

The task is to check this assertion, modulo 10. Lets call    i -> j   a transition if    i   is the last decimal digit of a prime, and    j   the last decimal digit of the following prime.

Considering the first one million primes.   Count, for any pair of successive primes, the number of transitions    i -> j   and print them along with their relative frequency, sorted by    i . You can see that, for a given    i ,   frequencies are not evenly distributed.

(Modulo 10),   primes whose last digit is   9   "prefer"   the digit   1   to the digit   9,   as its following prime.

Do the same for one hundred million primes.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Prime conspiracy step by step in the Java programming language

The provided Java code investigates a peculiar phenomenon in the distribution of prime numbers, known as "prime conspiracy." It analyzes the frequencies of consecutive prime numbers ending with specific digits and determines if there are any patterns or correlations.

  1. Main Method:

    • It sets the desired limit of prime numbers to find (limit) and a higher limit (sieveLimit) for a sieve of Eratosthenes to generate the required prime numbers.
    • Initializes a 2D array (buckets) with dimensions 10x10 to store the counts of prime numbers with each combination of a previous digit and a current digit (0 to 9).
  2. Prime Number Generation:

    • It starts from the prime number 3 and iteratively checks each odd number up to the limit.
    • For each prime number n found, it extracts the last digit (digit) and increments the count in the buckets array accordingly. The previous digit is stored to track the pattern of last digits.
    • This loop continues until primeCount reaches the desired limit.
  3. Sieve of Eratosthenes:

    • The sieve method is a helper function that generates a boolean array (composite) of size limit, where true indicates non-prime numbers.
    • It starts by considering 0 and 1 as non-prime and iterates up to the square root of limit.
    • For each prime number n, it marks its multiples as non-prime by setting composite[k] to true, where k ranges from n * n to limit.
  4. Results Output:

    • After the prime numbers are generated and their counts are stored in the buckets array, the code iterates through the 2D array and prints the counts for each combination of previous digit and current digit.
    • It calculates and displays the percentage of prime numbers with a specific last digit to a specified decimal place.
  5. Prime Conspiracy Analysis:

    • The output of this program can reveal patterns in the distribution of last digits of prime numbers. If there are notable differences in these distributions, it suggests that there may be some underlying "conspiracy" in the prime number sequence.

In summary, this code is a demonstration of the prime conspiracy theory. It investigates the correlation between the last digit of a prime number and the last digit of the subsequent prime number. By studying the distribution of these last digits, researchers aim to uncover potential patterns or anomalies in the seemingly random sequence of prime numbers.

Source code in the java programming language

public class PrimeConspiracy {

    public static void main(String[] args) {
        final int limit = 1000_000;
        final int sieveLimit = 15_500_000;

        int[][] buckets = new int[10][10];
        int prevDigit = 2;
        boolean[] notPrime = sieve(sieveLimit);

        for (int n = 3, primeCount = 1; primeCount < limit; n++) {
            if (notPrime[n])
                continue;

            int digit = n % 10;
            buckets[prevDigit][digit]++;
            prevDigit = digit;
            primeCount++;
        }

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (buckets[i][j] != 0) {
                    System.out.printf("%d -> %d : %2f%n", i,
                            j, buckets[i][j] / (limit / 100.0));
                }
            }
        }
    }

    public static boolean[] sieve(int limit) {
        boolean[] composite = new boolean[limit];
        composite[0] = composite[1] = true;

        int max = (int) Math.sqrt(limit);
        for (int n = 2; n <= max; n++) {
            if (!composite[n]) {
                for (int k = n * n; k < limit; k += n) {
                    composite[k] = true;
                }
            }
        }
        return composite;
    }
}


  

You may also check:How to resolve the algorithm Playing cards step by step in the Quackery programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the Perl programming language
You may also check:How to resolve the algorithm Generate random chess position step by step in the R programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Halt and catch fire step by step in the 11l programming language