Open-access mathematical research insights
About Contact
Home / Free Stuff

Hardy-Littlewood Conjecture (C++)

Patterns of primes continue forever - even as they thin out

The Conjecture

The Hardy-Littlewood conjecture essentially says that even though primes grow sparse as numbers grow large, they will continue to appear in pairs or other patterns forever. This shouldn't really be true because they are in general thinning out as the numbers grow large. So it is a key feature we can explore.

Prime Tuples

They use something called Prime Tuples - sets of primes arranged in certain ways. Examples include:

The highest known prime quadruplet has $p = 667,674,063,382,677 \times 2^{33608} - 1$

Why This Matters

The patterns continue forever according to Hardy and Littlewood, and because that is the case it probably means something helpful.

Because primes do not escape from each other at large $N$, there is something causing them to come back as pairs, cousins, and other patterns. That thing is Riemann's Zeta function.

Twin prime spacings visualization
Twin prime spacings - the gaps don't always grow larger

C++ Implementation

This code generates twin prime pairs and tracks the spacing between consecutive pairs. The key insight: as numbers get larger, the gaps between twin prime pairs do not always increase.

C++
#include <iostream>
#include <vector>
#include <iomanip> // For formatting

void generateTwinPrimeSpacings(int limit) {
    // Sieve of Eratosthenes
    std::vector<bool> isPrime(limit + 1, true);
    isPrime[0] = isPrime[1] = false;

    for (int i = 2; i * i <= limit; ++i) {
        if (isPrime[i]) {
            for (int j = i * i; j <= limit; j += i) {
                isPrime[j] = false;
            }
        }
    }

    // Store twin prime pairs and their starting points
    std::vector<int> twinPrimeStarts;
    for (int i = 2; i <= limit - 2; ++i) {
        if (isPrime[i] && isPrime[i + 2]) {
            twinPrimeStarts.push_back(i);
        }
    }

    // Print table header
    std::cout << "Twin Prime Pairs and Spacings up to " << limit << ":\n";
    std::cout << "--------------------------------------------\n";
    std::cout << "Prime 1 | Prime 2 | Spacing | Change\n";
    std::cout << "--------|---------|---------|-------\n";

    // Print first pair without spacing or change
    if (!twinPrimeStarts.empty()) {
        std::cout << std::setw(7) << twinPrimeStarts[0] << " | "
                  << std::setw(7) << twinPrimeStarts[0] + 2 << " | "
                  << std::setw(7) << "-" << " | "
                  << std::setw(5) << "-" << "\n";
    }

    // Print subsequent pairs with spacing and change
    for (size_t i = 1; i < twinPrimeStarts.size(); ++i) {
        int currentSpacing = twinPrimeStarts[i] - twinPrimeStarts[i - 1];
        std::string change = "-";

        // Calculate change in spacing (skip for first comparison)
        if (i > 1) {
            int prevSpacing = twinPrimeStarts[i - 1] - twinPrimeStarts[i - 2];
            if (currentSpacing > prevSpacing) {
                change = "+";
            } else if (currentSpacing < prevSpacing) {
                change = "-";
            } else {
                change = "="; // Equal spacing
            }
        }

        std::cout << std::setw(7) << twinPrimeStarts[i] << " | "
                  << std::setw(7) << twinPrimeStarts[i] + 2 << " | "
                  << std::setw(7) << currentSpacing << " | "
                  << std::setw(5) << change << "\n";
    }
}

int main() {
    int limit = 1000; // Adjustable limit
    generateTwinPrimeSpacings(limit);
    return 0;
}

Compiling & Running

Terminal
# Compile
g++ -std=c++17 -O2 hardy_littlewood.cpp -o hardy_littlewood

# Run
./hardy_littlewood

# Increase the limit in the code to see more pairs!

Related Resources

Stay Updated

Get weekly digests of new research insights delivered to your inbox.