Hello all, I have a simple code which computes applies the Chinese Remainder theorem (for a single variable) given three input congruence equations to solve for x; not very elegant but it works:
My query, is it possible to make this more generalised and vary the number of input equations and give the user an option to specify the number of equations? For example, one may have 2, 4 or more equations. Any pointers would be helpful. Thanks Lester clear // Chinese Remainder Theorem (CRT) for 3 congruence equations// e.g. x ≡ 3 mod 5; x ≡ 1 mod 7; x ≡ 6 mod 8// result ≡ 78 mod 280 m=evstr(x_dialog(['Moduli values: '],'1'));r=evstr(x_dialog(['Remainder values: '],'1'));m1=m(1); m2=m(2); m3=m(3);r1=r(1); r2=r(2); r3=r(3);n=evstr(x_dialog(['Number of iterations: '],'1')); M=m1*m2*m3;M1=M/m1; M2=M/m2; M3=M/m3; for i=1:n if modulo(((M1*i)-1),m1) == 0 then M1_inv=i; end if modulo(((M2*i)-1),m2) == 0 then M2_inv=i; end if modulo(((M3*i)-1),m3) == 0 then M3_inv=i; endend x1=(r1*M1*M1_inv);x2=(r2*M2*M2_inv);x3=(r3*M3*M3_inv); x = modulo((x1 + x2 + x3), M); messagebox(['x ≡ '+string(r1)'+' mod '+string(m1)' ... 'x ≡ '+string(r2)'+' mod '+string(m2)' ... 'x ≡ '+string(r3)'+' mod '+string(m3)' ... 'The solution for (x): '+string(x)+' mod '+string(M)'], ... 'Solution of 3 congruence equations')
_______________________________________________ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users