I’ve made a prime finder in C++ that writes primes into a .txt file, but it crashed after finding the 102,144,001th (2,084,058,601). It also stores the found primes in a vector, so it won’t have to divide by every single number lesser than the currently checked number’s square root, only the prime ones. It resizes the vector that stores the primes after every (10,240n+1)th found prime, and 102,144,001 is 10,240n+1, so it crashed while resizing. I use a vector unsigned __int64, so it used about 780 megabytes when it crashed, but I have 8 GB RAM. Should I use vector.reserve()? (I don’t want another crash after 50 minutes…)
Share
There are only 4730 primes less than the square root of 2084058601. If you are running out of memory while storing that small amount of data, you’re doing something wrong.
I recently wrote a prime generator and used it to sum the first billion primes. It uses a segmented Sieve of Eratosthenes, and adds additional sieving primes at the beginning of each segment. Memory use is constant for the size of the bit array and grows slowly for the sieving primes. You might want to take a look at it.