1

If we have a value $x$ that is known modulo all primes less than $p$, how quickly can we determine its value?

In other words, we know $x \bmod 2$, $x \bmod 3$, $x \bmod 5$, etc... We also know that $x$ is a natural number or $0$, i.e. $x$ is not negative.. How quickly, asymptotically, can we determine $x$, assuming $x$ is uniquely determined by the set of primes used? I mean here that $x < 2 \cdot 3 \cdot 5 \cdot \cdots p$. I'm looking for bounds in terms of $p$.

Matt Groff
  • 6,117
  • Is this specifically about computational complexity? – JustKevin Dec 15 '16 at 17:47
  • @JustKevin: It is about computational complexity, but I saw it as more of a math question, than computational complexity. – Matt Groff Dec 15 '16 at 17:50
  • Are you familiar with mixed radix reps such as $, n = n_0 + 2(n_1 + 3(n_2 + 5(n_3 + \cdots))),?\ $ These may be more natural depending on you application. – Bill Dubuque Dec 15 '16 at 19:52
  • @BillDubuque: I'd seen the term, but didn't know it. I'm probably stuck with using the congruences in the question, but I will definitely give mixed radix representation some thought. This is for an algorithm I'm designing, and my current method is hard to change. Thank you for your interest in this question, and for your ideas. I wasn't sure where to post this question, but I'm very thankful for all the ideas here. :-) – Matt Groff Dec 15 '16 at 21:12
  • @MattGroff If you describe what you're using this for then you might get other suggestions too. One place to lean about such is Knuth's TAOCP v.2 Seminumerical Algorithms. – Bill Dubuque Dec 15 '16 at 21:28
  • @BillDubuque: I'm using this for an algorithm in a research paper that I hope to one day publish, and I'm hesitant to give too many of my ideas away, without having published them first. I still don't know enough about my ideas yet to even know if they're worth publishing, and I've never published before. I would like to share my ideas, but again, I don't want to ruin my chance at publication, where I figure I can share my ideas. So I find myself wanting to share, but I believe I shouldn't. I hope this makes sense - some day I hope to share more... – Matt Groff Dec 15 '16 at 21:39

2 Answers2

3

The Chinese Remainder Theorem says that given relatively prime $\left\{p_i\right\}_{i=1}^n$ and any $\left\{x_i\right\}_{i=1}^n$ we can find an $x$ that satisfies $$ x\equiv x_i\pmod{p_i}\tag{1} $$ for all $i$ and that $x$ is unique mod $P=\prod\limits_{i=1}^np_i$.

To facilitate solving $(1)$ for each given $\left\{x_i\right\}_{i=1}^n$, we can compute $\left\{u_i\right\}_{i=1}^n$ so that $$ \begin{align} u_i&\equiv1\pmod{p_i}\\ u_i&\equiv0\pmod{P/p_i} \end{align}\tag{2} $$ by solving the equation $$ a_ip_i+b_iP/p_i=1\tag{3} $$ using the Extended Euclidean Algorithm, and setting $u_i=b_iP/p_i$.

Then, for any $\left\{x_i\right\}_{i=1}^n$, $$ x=\sum_{i=1}^nx_iu_i\tag{4} $$ satisfies $(1)$.

robjohn
  • 345,667
  • It would be helpful to state explicitly that you are simply applying one common form of CRT= Chinese Remainder Theorem. – Bill Dubuque Dec 15 '16 at 19:06
  • 1
    @BillDubuque: I am not sure what you are suggesting. $(3)$ is the solution to the CRT as given in $(4)$. We can do most of the work ahead of time if we know the set of primes. – robjohn Dec 15 '16 at 19:20
  • The point is that many readers probably would not have realized that it is nothing but standard CRT without explicit mention. And, yes, of course one does not have to recompute what remains fixed in subsequent applications. – Bill Dubuque Dec 15 '16 at 19:28
  • I have added a mention that $(4)$ is the CRT and given a link. – robjohn Dec 15 '16 at 19:32
  • I think it would be much clearer for beginners if you mentioned at the start that you are going to solve that system by the standard CRT method (with a link). thus providing context for what follows. – Bill Dubuque Dec 15 '16 at 19:34
  • @BillDubuque: I have reorganized the answer, but it says exactly the same thing, except now I describe the CRT. – robjohn Dec 15 '16 at 20:45
  • Great! Readers of the long linked Wikipedia article should note that this construction of the solution is section titled "Existence (direct construction)." See also this answer, as well as most any textbook in elementary number theory. There are almost surely some expositions on this site that are better than the Wikipedia article. – Bill Dubuque Dec 15 '16 at 20:51
1

I have an equation for this. I'm not sure about the complexity or if it's unique and I use some awkward notation. But again we can't find a specific value for $x$ based on this information without all the primes, unless $p$ is the largest prime less than $x$ in which case those residues can't be chosen -- they are determined by the value for $x$. However, with $x < p\#$, I will oblige.

Let $x \equiv x_i\pmod{p_i}$ and $x_i \in [0, p_i)$

$$x \equiv \Sigma_{p_i<p}{\{x_i(\frac{p\#}{p_i})[(\frac{p\#}{p_i})^{-1}_{\pmod{p_i}}]\}} \pmod{p\#}$$

Because $p\# \equiv 0 \pmod{p_i}$ for all the primes less than $p$, $\frac{p\#}{p_i} \not \equiv 0 \pmod{p_i}$, and rings of integers mod primes have integer inverses,

$$ (\frac{p\#}{p_i})[(\frac{p\#}{p_i})^{-1}_{\pmod{p_i}}] \equiv 1 \pmod{p_i}$$ $$ (\frac{p\#}{p_i})[(\frac{p\#}{p_i})^{-1}_{\pmod{p_i}}] \equiv 0 \pmod{p_j}$$

where $p_j \ne p_i$ and both are less than $p$.

I want it to be clear that the inverse taken is from the ring of integers mod $p_i$. And therefore we've found $x$, conserving each residue.

Finding the inverse requires roughly i/2 steps, because the inverses are symmetric around (p+1)/2, and that step is carried out i times. And finding primorial is i steps, but that has to be done at the beginning, no matter what.

So $O(n + n^2/2)$ is my best guess for complexity.

user56983
  • 198