21

Let $n$ be an odd composite number. I'm trying to compute $$ f(n)=\prod_{n/2<p<n}p\pmod n $$ where $p$ ranges over the primes in the indicated region.

Can this be done (significantly) faster than multiplying and reducing one-by-one?

Charles
  • 32,999
  • Since you need to identify the primes, I don't see a significantly quicker way of achieving this. I didn't see any obvious patterns calculating your $f$ for small numbers to $1000$. – Joffan Mar 07 '15 at 16:09
  • @Joffan: It's not obvious to me that I need to identify primes -- if you can prove this I would certainly accept that as an answer! – Charles Mar 07 '15 at 17:33
  • I was assuming that your use of $p$ means that you are only multiplying the primes in that range (which I still believe is your intention) but I just want to confirm that is actually the case. Otherwise, obviously, $f(n)=0$ except for $f(9)$. – Joffan Mar 07 '15 at 18:00
  • @Joffan: Yes, the product ranges over the primes. (Maybe I should edit to clarify?) But just like we don't need to find all the numbers up to p-1 to compute the product (p-1)! mod p, it's not clear to me that this product requires finding all the primes from n/2 to n. See also http://math.stackexchange.com/q/28238/1778 where a sum over primes can be computed faster than enumerating the primes. – Charles Mar 07 '15 at 18:20
  • Yes, that is what I understood from your initial response, and I doubt I can prove there is no clever way to avoid identifying the primes in the range :-) – Joffan Mar 07 '15 at 18:32
  • Can't you use the same DDR algorithm from your other answer to compute the number of primes in each residue class modulo each prime factor of $n$ less than $x=n$ and $x=n/2$? That should be faster if $n$ doesn't have one large prime factor. – Zander Mar 08 '15 at 01:45
  • @Zander: That would be faster, in principle, if $n$ was somewhat more than $\sqrt n/2$-smooth. In practice the combinatorial algorithms are faster for regions in which you could practically compute this $q-1$ times so you'd need more like $n^{1/3}$-smooth. This still describes a positive fraction of integers -- about 4.8% -- but not enough to be all that useful. (Still, feel free to clean it up and post it as an answer, since I have no others at the moment!) – Charles Mar 08 '15 at 18:32
  • Doesn't your answer to http://math.stackexchange.com/a/86559/222498 also work here? I.e. calculate $\prod_{0 \leq i < q} i^{(\pi(n, q, i) - \pi(n, q, i))}$ for all $q < 2 \log(n)$ and use the Chinese Remainder Theorem? – Uri Granta Mar 11 '15 at 14:33
  • 1
    @UriZarfaty: Only if $n$ is unusually smooth, see my response to Zander above. – Charles Mar 11 '15 at 14:55
  • I think, part of the problem, with comparing the two, is you eliminate some n-tuples easily from the other problem. Simply by checking if N>M/2, and that M doesn't contain a power greater than k, ( which if $k\gt log_2(m)$, is guaranteed as no prime power can reach that high then.) . In this problem, all of n's prime factors aren't ever in the primes computed. we do know products of two of them have to exceed $\frac{n^2}{4}$ though. –  Jun 28 '17 at 01:04

1 Answers1

1

If you already have the primes, and this has boiled down to a computer science question (since you mention speed of computation), you can compute the product in parallel.

For example, if you had two cores you could compute the product of the 1st element, 3rd element, 5th element, etc. on the first core, and the product of the 2nd element, 4th element, 6th element, etc. on the second core.

Since $ab\ mod(n) = (a\ mod(n))*(b\ mod(n))\ mod(n)$, you can then just multiply the results from both cores once they are done, with little need to worry about concurrency issues.

  • The parallel situation is even better than that: you can do the sieving for primes in parallel, using $\sqrt n$ machines to sieve out regions of length $~\sqrt n$ (or appropriate arithmetic progressions) and then multiplying and reducing, for a total of $O(\sqrt n)$ time over $\sqrt n$ cores. So you don't need to have the primes ahead of time. In the extreme you could take $n$ cores, have each one perform a primality test in $O(\log^{4+\varepsilon}n)$ time, and then multiply-and-reduce with neighbors $\lceil\log n/\log 2\rceil$ times for a total of $O(\log^{4+\varepsilon}n)$ time. – Charles May 24 '17 at 19:33
  • 1
    But I'm not really interested in the parallel version, especially since it's just the same as the serial one. I'm wondering if there is a different approach entirely. – Charles May 24 '17 at 19:34
  • Oh, I'm sorry. :( Should I delete the answer as it doesn't answer your question – Ninja_Coder May 25 '17 at 04:29