How to resolve the algorithm Erdös-Selfridge categorization of primes step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Erdös-Selfridge categorization of primes step by step in the Java programming language

Table of Contents

Problem Statement

A prime p is in category 1 if the prime factors of p+1 are 2 and or 3. p is in category 2 if all the prime factors of p+1 are in category 1. p is in category g if all the prime factors of p+1 are in categories 1 to g-1. The task is first to display the first 200 primes allocated to their category, then assign the first million primes to their category, displaying the smallest prime, the largest prime, and the count of primes allocated to each category.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Erdös-Selfridge categorization of primes step by step in the Java programming language

The provided Java program implements the Erdős–Selfridge theorem, which states that there are infinitely many prime numbers of the form (p\pm 1), where (p) is a prime number. The program finds the category of each of the first million prime numbers, where the category of a prime number is defined as the largest exponent of a prime divisor of (p\pm 1).

The program starts by generating a list of the first million prime numbers using a PrimeGenerator object. It then iterates through this list of primes and calculates the category of each prime. The category of a prime is calculated by finding the largest prime factor of (p\pm 1), and then adding 1 to the exponent of that prime factor.

Once the category of each prime has been calculated, the program groups the primes by their category and prints out the first 200 primes in each category. It also prints out the total number of primes in each category.

Here is a breakdown of the main components of the program:

PrimeGenerator: This class is used to generate a list of prime numbers. It uses the Sieve of Eratosthenes algorithm to generate a list of prime numbers up to a specified limit.

ErdősSelfridge: This class contains the main logic for implementing the Erdős–Selfridge theorem. It has a method called getPrimesByCategory that takes a limit as an argument and returns a map of categories to lists of primes. The map keys are the categories, and the map values are lists of the primes in each category.

getCategory: This method calculates the category of a prime number. It takes an index as an argument and returns the category of the prime number at that index in the list of primes. The category of a prime number is calculated by finding the largest prime factor of (p\pm 1), and then adding 1 to the exponent of that prime factor.

getIndex: This method returns the index of a prime number in the list of primes. It takes a prime number as an argument and returns the index of that prime number in the list.

main: This method is the entry point of the program. It creates an ErdősSelfridge object and calls the getPrimesByCategory method to get a map of categories to lists of primes. It then prints out the first 200 primes in each category, as well as the total number of primes in each category.

Source code in the java programming language

import java.util.*;

public class ErdosSelfridge {
    private int[] primes;
    private int[] category;

    public static void main(String[] args) {
        ErdosSelfridge es = new ErdosSelfridge(1000000);

        System.out.println("First 200 primes:");
        for (var e : es.getPrimesByCategory(200).entrySet()) {
            int category = e.getKey();
            List<Integer> primes = e.getValue();
            System.out.printf("Category %d:\n", category);
            for (int i = 0, n = primes.size(); i != n; ++i)
                System.out.printf("%4d%c", primes.get(i), (i + 1) % 15 == 0 ? '\n' : ' ');
            System.out.printf("\n\n");
        }

        System.out.println("First 1,000,000 primes:");
        for (var e : es.getPrimesByCategory(1000000).entrySet()) {
            int category = e.getKey();
            List<Integer> primes = e.getValue();
            System.out.printf("Category %2d: first = %7d  last = %8d  count = %d\n", category,
                              primes.get(0), primes.get(primes.size() - 1), primes.size());
        }
    }

    private ErdosSelfridge(int limit) {
        PrimeGenerator primeGen = new PrimeGenerator(100000, 200000);
        List<Integer> primeList = new ArrayList<>();
        for (int i = 0; i < limit; ++i)
            primeList.add(primeGen.nextPrime());
        primes = new int[primeList.size()];
        for (int i = 0; i < primes.length; ++i)
            primes[i] = primeList.get(i);
        category = new int[primes.length];
    }

    private Map<Integer, List<Integer>> getPrimesByCategory(int limit) {
        Map<Integer, List<Integer>> result = new TreeMap<>();
        for (int i = 0; i < limit; ++i) {
            var p = result.computeIfAbsent(getCategory(i), k -> new ArrayList<Integer>());
            p.add(primes[i]);
        }
        return result;
    }

    private int getCategory(int index) {
        if (category[index] != 0)
            return category[index];
        int maxCategory = 0;
        int n = primes[index] + 1;
        for (int i = 0; n > 1; ++i) {
            int p = primes[i];
            if (p * p > n)
                break;
            int count = 0;
            for (; n % p == 0; ++count)
                n /= p;
            if (count != 0) {
                int category = (p <= 3) ? 1 : 1 + getCategory(i);
                maxCategory = Math.max(maxCategory, category);
            }
        }
        if (n > 1) {
            int category = (n <= 3) ? 1 : 1 + getCategory(getIndex(n));
            maxCategory = Math.max(maxCategory, category);
        }
        category[index] = maxCategory;
        return maxCategory;
    }

    private int getIndex(int prime) {
       return Arrays.binarySearch(primes, prime);
    }
}


  

You may also check:How to resolve the algorithm Empty program step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Zumkeller numbers step by step in the Typescript programming language
You may also check:How to resolve the algorithm Tau function step by step in the R programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the Fish programming language