20160329, 13:12  #1 
Apr 2014
Marlow, UK
2^{3}×7 Posts 
CRT optimisation?
Apologies in advance for what is probably a rather naïve question. I'm playing with a factoring algorithm in which CRT calculations are one of the bottlenecks. I have used an optimisation to the CRT calculation, which improves things slightly. I have never seen this optimisation mentioned in the literature, and was wondering why, as it is a very obvious one: Rather than solving:
x = r_{0} (mod m_{0})we can solve xr_{0} = 0 (mod m_{0})meaning one modular inversion can be avoided due to the 0 residue in the modified first congruence. For example, in the case of just 2 congruences, this leads to: x = r_{0} + m_{0}^{1}_{m[SUB]1}[/SUB] m_{0} (r_{1}r_{0}) (mod m_{0}m_{1})rather than the usual: x = m_{1}^{1}_{m[SUB]0}[/SUB]m_{1}r_{0} + m_{0}^{1}_{m[SUB]1}[/SUB]m_{0}r_{1} (mod m_{0}m_{1})Do the disadvantages (initial extra addition plus extra subtraction per noninitial congruence, and the possible complications of (r_{i}r_{0}) going negative) outweigh the advantages in general? Just curious... (and apologies for using = rather than the congruence symbol, which I can't find!) Last fiddled with by mickfrancis on 20160329 at 13:19 Reason: Formatting 
20160329, 13:36  #2 
"Forget I exist"
Jul 2009
Dumbassville
2^{6}·131 Posts 

20160329, 13:39  #3 
Apr 2014
Marlow, UK
2^{3}×7 Posts 

20160329, 14:20  #4 
Tribal Bullet
Oct 2004
2^{4}×13×17 Posts 
Are you assuming m0 is the smallest modulus in the set? I would think you risk messing up the CRT otherwise.
I don't have my copy of Knuth handy, but this looks a little like the fast(er) CRT that he describes. 
20160329, 14:52  #5 
Apr 2014
Marlow, UK
56_{10} Posts 
I'm not sure why it would be a problem  it's really just modular subtraction on the left and subtraction in a Residue Number System on the right isn't it? (I'm probably missing something here though...). I'd be interested to see the Knuth algorithm...

20160329, 16:46  #6 
Dec 2012
The Netherlands
5×331 Posts 
It's an exercise in Knuth (The Art of Computer Programming volume 2) just after his discussion of the Garner algorithm.

20160329, 18:49  #7 
Apr 2014
Marlow, UK
70_{8} Posts 

20160330, 00:38  #8 
Tribal Bullet
Oct 2004
2^{4}×13×17 Posts 
The problem I was thinking of was that in general (a mod b) mod c is not the same as (a mod c) mod b. So if your m0 was larger than m1, then (r1  r0) mod m1 may not be the same as (r1  (r0 mod m1)) mod m1. I could be overthinking it though.

20160330, 02:08  #9  
"Forget I exist"
Jul 2009
Dumbassville
2^{6}·131 Posts 
Quote:
(30 mod 5) mod 7 > 0 mod 7 (30 mod 7) mod 5 > 2 mod 5 all we did was change the order of the modulo operations and it changes what we get back. I think jasonp is overthinking it if CRT is chinese remainder theorem as the mod is never done twice and if you subtract the same thing from two things that are congruent they stay congruent. in the two congruences you give ( unless you meant m0 in one of them) you can think of them as polynomials r1r0 can be (m1*y+r1)(m1*z+r2). though I still don't see how you can cross values mod the m's like that. if you remember that any value mod another number is always congruent to itself you can rewrite the second as the first. Last fiddled with by science_man_88 on 20160330 at 02:11 

20160330, 10:20  #10 
Dec 2012
The Netherlands
5×331 Posts 
May I suggest that the modern algebraic way of looking at this offers some advantages here?
If we are working with integers modulo 5, for example, then we regard integers a and b as equivalent if (and only if) 5 divides ab, so we have 5 equivalence classes: {...,15,10,5, 0,5,10,15,...} {...,14,9,4, 1,6,11,16,...} {...,13,8,3, 2,7,12,17,...} {...,12,7,2, 3,8,13,18,...} {...,11,6,1, 4,9,14,19,...} For any integer a, we write \(\bar{a}\) to denote the equivalence class of a, i.e. the entire set of all integers equivalent to a modulo 5 (this is the set in the above list which a appears in). We then define addition and multiplication on these sets by: \[ \begin{eqnarray*} \bar{a}+\bar{b} & = & \overline{a+b} \\ \bar{a}\cdot\bar{b} & = & \overline{a\cdot b} \end{eqnarray*} \] for any integers a & b, and show that this is welldefined (i.e. the definition does not depend on the representative elements chosen). Thus, for example, we can write \(\bar{4}^2=\overline{1}^2=\bar{1}\). Viewed this way, we are not tempted to write something like "30 mod 5 mod 7" because 30 mod 5 is then the set \(\overline{30}=\bar{0}\) and not all elements of the set have the same remainder on division by 7, so the final "mod 7" is not a valid operation. 
Thread Tools  
Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Data Centre Energy Consumption Optimisation  pinhodecarlos  Lounge  15  20190315 14:19 
An opportunity for search engine optimisation  fivemack  Aliquot Sequences  6  20151117 20:31 
Basic optimisation question  fivemack  Puzzles  6  20080408 13:50 
HT and all kind of other optimisation thread  hhh  Prime Sierpinski Project  2  20060406 09:33 