Complexity of prime-finding (32/64 bit fixed-length ints!) algorithm

60 Views Asked by At

What's the time complexity for finding all primes up to N using the following algorithm:

primes = [2]
for pc in range(3, N, 2):
  for p in primes:
    if p * p > pc:
      primes.add(pc)
      break
    if pc mod p == 0:
      break

What I found is basically an estimate which can be expressed as an integral $\int{\frac{\sqrt{x}dx}{\log{x}}} = Ei(\frac{3}{2}\log x)$ which in turn according to comments and answers to this question could be approximated using $E_1(x) \approx \frac{1}{x e^x}$. Since $E_1(x) = -Ei(-x)$,

$$Ei(1.5 \log x) = -E_1(-1.5 \log x) = \frac{-e^{1.5 \log x}}{-1.5 \log x} \approx \frac{e^{1.5 \log x}}{\log x} = \frac{x^{1.5}}{\log x}$$

  • Are those calculations correct?
  • The estimate is rough, any ideas how to improve it?
  • Are there any better algorithms for fixed precision numbers (O(1) arithmetic operations) which are also simple to implement?