I am willing to get a linear output of a product in a ring Z/nZFor that I have successfully implemented a code that does a Product in a moduloHowever, it is not optimized to output the steps smoothly:/* Product of n1 x n2 x n3 modulo M */var numbers=[n1,n2,n3];var modulo= M;var AProd=1;var BProd=numbers[0];var accu=0;var nProd=true;var iProd=0;var kProd=0;var tritemp = 0;    function ProductModulo() { if (AProd == 1 || iProd == 0) {     nProd = false; } if (nProd && tritemp == 0) {     if (accu > 1) {         tritemp = accu;     } else {         tritemp = AProd;     } } if (nProd && 10 * tritemp <= accu) {     accu = 10 * tritemp;     nProd = false;     tritemp = 0; } if (nProd) {     accu += tritemp; //arbitrary } if (AProd == 1) {     kProd = BProd.toString().length; } else if (!nProd) {     if (tritemp == 0) {         tritemp = accu + Number(AProd.toString()[iProd]) * Number(BProd.toString()[kProd]) * Math.pow(10, BProd.toString().length - 1 - kProd);     }     accu += 50; //arbitrary     if (accu >= tritemp) {         accu = tritemp;         tritemp = 0;         kProd++;     } } if (kProd == BProd.toString().length) {     accu = accu % modulo;     iProd++;     kProd = 0;     nProd = true;     tritemp = 0; } if (iProd == AProd.toString().length) {     if (AProd == 1) {         AProd = BProd;     } else {         AProd = accu;     }     numbers.splice(0, 1);     iProd = 0;     if (numbers.length > 0) {         BProd = numbers[0];         accu = 0;     } } if (numbers.length == 0) {     //display result accu, calculation is done     console.log(Final Result:  + accu); } else {     // (could display accu)     ProductModulo(); }}EDIT Here is a test page to see it in actionThe code works as follow:step 1: We take the current number in accumulator and multiply by 10step 2: We take the first digit (of AProd) and multiply by Bprodstep 3: we add result of step 2 to result of step 1 and modulo M of itstep 4: Same as Step 1, 2 and 3 with the next digit of AProd...When all Aprods digits have been done we do the same between the result and n3So, this code works but the problem is I would like a more linear way of visualizing the whole process. For instance I already show all the steps of additions instead of outputing the result directly.Is there a smooth way, of increasing the accu, and is it possible to work on n1 and n2 and n3 at the same time instead of doing, (n1*n2) and then *n3?Note: I am only interested with numbers positive and co-prime with the modulo
Slow multiplication in a modulo
javascript;mathematics