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

Chebyshev's Bias (C++)

A slightly uneven spread; pushed by Riemann's Zeta

The Bias in Prime Distribution

Chebyshev's Bias is likely to be quite important when Riemann's Hypothesis is eventually proven or disproven. It's one of the best simple examples showing that there's some asymmetric disturbance happening with prime numbers.

All primes except 2 are odd. When divided by 4, they leave a remainder of either 1 or 3. They can be categorized:

In theory, there should be a perfectly equal spread between these two groups. But the spread is not equal. The $4k + 3$ group normally wins.

Chebyshev's Bias visualization
Chebyshev's Bias - the 4k+3 primes consistently lead

It's the Riemann Zeta function that is giving $4k + 3$ its push. Although we can't prove it yet, this connection to the zeros of the zeta function is believed to be the underlying cause.

C++ Implementation

The code takes the first 50 million primes and computes the bias. You'll need to point the code to a file containing primes (like 50mprimes.txt which you can download from our resources).

C++
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include <iomanip>

using namespace std;

int main() {
    string filename = "/path/to/50mprime.txt";
    ifstream file(filename);

    if (!file.is_open()) {
        cout << "Failed to open the file." << endl;
        return 1;
    }

    unordered_map<int, int> digit_counts;
    int total_count = 0;

    int prime;
    while (file >> prime) {
        int last_digit = prime % 10;
        if (last_digit == 3 || last_digit == 7 || last_digit == 9 ||
            last_digit == 1 || last_digit == 2 || last_digit == 5) {
            digit_counts[last_digit]++;
            total_count++;
        }
    }

    file.close();

    cout << "Digit\tCount\tPercentage" << endl;

    for (const auto& pair : digit_counts) {
        int digit = pair.first;
        int count = pair.second;
        double percentage = (count * 100.0) / total_count;

        cout << digit << "\t" << count << "\t";
        cout << fixed << setprecision(8) << percentage << "%" << endl;
    }

    return 0;
}

Sample Output

Chebyshev's Bias output table
Output showing the distribution of last digits

You could modify the code to exclude remainders 1 and 5 from the print for cleaner output.

Related Resources

Stay Updated

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