Table of biggest prime factor of every integer in 2 ... 10^K

44 Views Asked by At

How would you precalculate a big table of the biggest prime factor of every integer in $2 \dots 10^K$? The goal would be to do it once for all, save it to a file, and reload it later for fast computations (involving smooth numbers). I tried with R:

library(gmp)    # the fastest factorization I've found on R, faster than number theory package "numbers"
v = sapply(2:10^7, function(x) as.integer(max(factorize(x))))
save(v, file="bpf.dat")

allowing later to load it with: load("bpf.dat").

But this is very slow (35 seconds for $K=6$, 390 seconds for $K=7$ , etc.). Which number-theoretic idea could be used to generate such a table faster?


Note: I've already looked at https://oeis.org/A006530.
Note2: This question has been removed in mathoverflow (because too obvious), but I wanted to post here question+answer thanks to the helpful comments that were provided.

1

There are 1 best solutions below

0
On BEST ANSWER

As mentioned in a comment of a now-deleted question, a modification of the sieve of Eratosthenes will produce this information very quickly, almost in linear time. Here is some C code to do it:

#include <iostream>
#include <vector>

const int MAX = 1000000000;

int main() {
    std::vector<int> largest(MAX);

    for (int i = 2; i < MAX; ++i)
        largest[i] = i;

    for (int i = 2; i < MAX; ++i)
        if (largest[i] == i)
            for (int j = i+i; j < MAX; j += i)
                largest[j] = i;

    for (int i = 2; i < MAX; ++i)
        std::cout << largest[i] << std::endl;
}