10

How many numbers $ N \le10^{10}$ are the product of $3$ distinct primes? I can realistically calculate any $\pi(n), n < 10^{15} $ but I don't think it's possible to list all primes $>10^8$ in memory. The limits aren't hard but are approximations. Obviously $\binom{\pi(10^{10})}{3}$ won't work, as that produces products way above $10^{10}$. I suspect a recursive solution may work, but I do not have experience in those methods, and three for loops is much too slow. Ideally, the solution is sublinear in time complexity and can be extended to $10^{11}, 10^{12}, 10^{13}, 10^{14}, ...$

Edit: A modified Sieve of Eratosthenes method similar to this answer may work, with three primes instead of one, but it requires $O(N)$ memory, which is not realistic.

qwr
  • 11,362
  • solving with a computer is really easy, would you like some code? – Asinomás Apr 08 '16 at 19:36
  • @CarryonSmiling I am more interested in the math methods, so $N$ can be extended to $10^{11}, 10^{12}, 10^{13}, 10^{14},...$ – qwr Apr 08 '16 at 19:41
  • 2
    You're aware you aren't going to find a method which allows you to find the answer with pencil and paper in a reasonable amount of time right? – Asinomás Apr 08 '16 at 19:44
  • @CarryonSmiling Yes I know. But I'm not very creative and 3 for loops is all I can think up. – qwr Apr 08 '16 at 19:45
  • If you don't mind using powers of $2$ instead of $10$ there is a way to approximate it using a very fast algorithm. – Mastrem Apr 08 '16 at 19:52
  • @Mastrem I'm interested in exact answers, that is where the challenge lies – qwr Apr 08 '16 at 19:54
  • @Mastrem if it helps answer my question please post it! – qwr Apr 08 '16 at 20:04
  • "exact answers" in terms of the $\pi$ function? And I presume "closed form" expressions, not anything with a summation? – 2'5 9'2 Apr 16 '16 at 22:18
  • @alex.jordan summations are fine, as long as a computer can run it in reasonable time. I have given some rough bounds. I don't think $O(n)$ memory or worse than $O(n)$ running time will be usable. – qwr Apr 16 '16 at 22:32
  • You've indicated a nested for loop counting incidents is not what you want. But a nested for loop is essentially a nested summation where you are summing 1 or 0 along the way. – 2'5 9'2 Apr 16 '16 at 22:50
  • @alex.jordan I believe nested for loops will be too slow without some tricks – qwr Apr 16 '16 at 23:04
  • This question belongs on programmers.SE, not math.SE. But this reeks of a DP problem - there is a significant problem that you face here in that you are demanding $<O(n)$ memory usage, but the very nature of your problem relies on the calculation of all primes in $[1, n]$, and being unable to keep primes in memory means that you have to recalculate them time and time again, which will impose significant runtime costs. Moreover, since factoring is in NP, you pretty much have to use this approach. – Pockets Apr 17 '16 at 07:33
  • @Pockets I disagree with your assertion that all primes from 1 to n need to be calculated - just look at the formula for $\pi(x)$ and you will see that in fact no primes are calculated at all, and it runs in far less than $O(n)$ time and memory. Counting primes is not the same as factoring, and this problem is an extension of counting. – qwr Apr 17 '16 at 08:27
  • There is no formula for $\pi(x)$; there are formulas to approximate it. You have made it explicitly clear that you want an exact solution, which even by partitioning the solution set requires the computation of all primes within some subset of $[1,n]$. – Pockets Apr 17 '16 at 08:35
  • 2
    @Pockets now you are plain wrong. There are formulas dating back to Legendre and I was referring to the widely known Meissel-Lehmer prime counting algorithm. I don't think it will be useful to continue this conversation. – qwr Apr 17 '16 at 16:59

1 Answers1

10

You'll need a list of primes out to around $71{,}000$. This is because if $n\lt10^{10}$ is a product of three distinct primes, say $n=pqr$ with $p\lt q\lt r$, then $q\lt\sqrt{10^{10}/p}\le10^5/\sqrt2\approx70{,}710.678$. Note also that $p\lt\sqrt[3]{10^{10}}\approx2154.43$, so there is a fairly straightforward brute-force algorithm that seems practical, at least for the specific number, $10^{10}$:

For each prime $p$ between $2$ and $2154$, consider the primes $q$ between $p$ and $10^5/\sqrt p$. For each of these, compute $\pi(\lfloor10^{10}/pq\rfloor)-\pi(q)$, which counts the number of available primes $r$ that are greater than $q$. There are only a few hundred primes $p$ and, for most of these, only a few thousand primes $q$, so the total number of times the $\pi$ function needs to be evaluated is almost certainly less than a million, which seems reasonable to ask a computer to do. A more careful analysis could possibly shave off another order of magnitude or so.

In sum, we can write the function the OP is after as

$$\pi_3(N)=\sum_{2\le p\le\sqrt[3]{N}}\left(\sum_{p\lt q\le\sqrt{N/p}}\left(\pi(\lfloor N/pq\rfloor)-\pi(q) \right)\right)$$

where the nested sums are understood to run over primes in the given ranges. The total number of summands is (substantially) less than $N^{1/3}N^{1/2}=N^{5/6}$. I'd be happy to see an approach that does better.

Barry Cipra
  • 81,321