May 2, 2025

Download the first 50 million primes

Also the first 37 billion primes for the very interested

DownloadOpen

You have a few options for getting prime lists for your programs. The main purpose is if you have a text file with a large block of confirmed primes you can work with them without having to generate the primes every time which saves hours.

The first 50 million primes can be downloaded here (in separate files - I would recommend then combining them). As Prime Pages says, you can generate them yourself but often people just want to download them and get going.

A much more extensive job has been done here at Project Gutenburg. Here you can download files containing all the primes up to 1 trillion. It's about 37 billion prime numbers. It will take a lot of disk space so I would probably do it only if you were a serious researcher; for most purposes the 50 million above will be enough.

Kim Walisch Github is also very good, with extensive program options for checking, counting and creating primes.

You can also easily generate them yourself . It is CPU intensive but takes only minutes to get large numbers.

#COMPILE
g++ -O2 -o generate_primes generate_primes.cpp

#RUN
./generate_primes


#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>

// Check if number is prime
bool isPrime(int num, const std::vector<int>& primes) {
    int sqrtNum = static_cast<int>(std::sqrt(num));
    for (int prime : primes) {
        if (prime > sqrtNum)
            break;
        if (num % prime == 0)
            return false;
    }
    return true;
}

int main() {
    std::ofstream outputFile("primes.txt");

    if (!outputFile) {
        std::cerr << "Failed to open primes.txt for writing." << std::endl;
        return 1;
    }

    int primeCount;
    std::cout << "How many prime numbers do you want to generate? ";
    std::cin >> primeCount;

    if (primeCount <= 0) {
        std::cerr << "Number must be positive." << std::endl;
        return 1;
    }

    std::vector<int> primes;
    primes.push_back(2);
    int currentNumber = 3;

    outputFile << 2 << std::endl;

    while (primes.size() < static_cast<size_t>(primeCount)) {
        if (isPrime(currentNumber, primes)) {
            primes.push_back(currentNumber);
            outputFile << currentNumber << std::endl;
        }
        currentNumber += 2; // Skip even numbers after 2
    }

    outputFile.close();

    std::cout << "Successfully generated " << primeCount << " prime numbers in primes.txt." << std::endl;

    return 0;
}