Fundamentals 5 min read

Finding Twin Primes Using Recursive Filtering in Java

The article explains how to generate prime numbers with a recursive filtering method, identify twin primes below 10,000, and includes Java implementations of bubble sort and insertion sort, along with a brief commentary on Groovy's advantages and a curated list of related technical articles.

FunTester
FunTester
FunTester
Finding Twin Primes Using Recursive Filtering in Java

The author, after reading the book "The Loneliness of Prime Numbers", wanted to investigate the distribution of twin primes and sought a more convenient method than traditional for‑loop sieves, opting for a recursive filtering approach.

They create a List starting from index 0 and recursively remove any element divisible by the current number, leaving only prime numbers; the core method is shown below:

static void get(List
list, int tt) {
    int num = list.get(tt);
    for (int i = tt + 1; i < list.size(); i++) {
        if (list.get(i) % num == 0) list.remove(i--);
    }
    if (list.size() > ++tt) get(list, tt);
}

Using the filtered list, the program computes the difference between adjacent elements to find twin primes (pairs of primes differing by 2) below 10,000; the complete code for this task is:

List
list = new ArrayList<>();
for (int i = 2; i < 10000; i += 2) {
    list.add(i);
}
get(list, 0);
for (int i = 0; i < list.size() - 1; i++) {
    Integer a = list.get(i);
    Integer b = list.get(i + 1);
    if (b - a == 2) outputData(TEST_ERROR_CODE, "Twin primes:", a + TAB + TAB + b);
}

The article also provides simple implementations of bubble sort and insertion sort for educational purposes:

public static void ff(int[] data) {
    for (int i = 0; i < data.length; i++) {
        for (int j = i; j > 0; j--) {
            if (data[j] < data[j - 1]) {
                int num = data[j];
                data[j] = data[j - 1];
                data[j - 1] = num;
            }
        }
    }
    output(changeArraysToList(data));
}

public static void ff1(int[] data) {
    for (int i = 0; i < data.length; i++) {
        for (int j = 0; j < data.length - i - 1; j++) {
            if (data[j] > data[j + 1]) {
                int num = data[j];
                data[j] = data[j + 1];
                data[j + 1] = num;
            }
        }
    }
    output(changeArraysToList(data));
}

A brief commentary follows on Groovy, noting its strong JVM compatibility with Java, low learning curve, and good IDE and Gradle support (the paragraph is filler and not directly related to the main topic).

Finally, the article lists a selection of technical and non‑technical articles (with links) for further reading, covering topics such as Java one‑line heart shape printing, Linux performance monitoring, test coverage, HTTP, and career advice.

JavaalgorithmrecursionsortingPrime Numberstwin primes
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.