How to resolve the algorithm Chinese remainder theorem step by step in the ZX Spectrum Basic programming language
How to resolve the algorithm Chinese remainder theorem step by step in the ZX Spectrum Basic programming language
Table of Contents
Problem Statement
Suppose
n
1
{\displaystyle n_{1}}
,
n
2
{\displaystyle n_{2}}
,
…
{\displaystyle \ldots }
,
n
k
{\displaystyle n_{k}}
are positive integers that are pairwise co-prime. Then, for any given sequence of integers
a
1
{\displaystyle a_{1}}
,
a
2
{\displaystyle a_{2}}
,
…
{\displaystyle \dots }
,
a
k
{\displaystyle a_{k}}
, there exists an integer
x
{\displaystyle x}
solving the following system of simultaneous congruences: Furthermore, all solutions
x
{\displaystyle x}
of this system are congruent modulo the product,
N
n
1
n
2
…
n
k
{\displaystyle N=n_{1}n_{2}\ldots n_{k}}
.
Write a program to solve a system of linear congruences by applying the Chinese Remainder Theorem. If the system of equations cannot be solved, your program must somehow indicate this. (It may throw an exception or return a special false value.) Since there are infinitely many solutions, the program should return the unique solution
s
{\displaystyle s}
where
0 ≤ s ≤
n
1
n
2
…
n
k
{\displaystyle 0\leq s\leq n_{1}n_{2}\ldots n_{k}}
.
Show the functionality of this program by printing the result such that the
n
{\displaystyle n}
's are
[ 3 , 5 , 7 ]
{\displaystyle [3,5,7]}
and the
a
{\displaystyle a}
's are
[ 2 , 3 , 2 ]
{\displaystyle [2,3,2]}
.
Algorithm: The following algorithm only applies if the
n
i
{\displaystyle n_{i}}
's are pairwise co-prime. Suppose, as above, that a solution is required for the system of congruences: Again, to begin, the product
N
n
1
n
2
…
n
k
{\displaystyle N=n_{1}n_{2}\ldots n_{k}}
is defined. Then a solution
x
{\displaystyle x}
can be found as follows: For each
i
{\displaystyle i}
, the integers
n
i
{\displaystyle n_{i}}
and
N
/
n
i
{\displaystyle N/n_{i}}
are co-prime. Using the Extended Euclidean algorithm, we can find integers
r
i
{\displaystyle r_{i}}
and
s
i
{\displaystyle s_{i}}
such that
r
i
n
i
s
i
N
/
n
i
= 1
{\displaystyle r_{i}n_{i}+s_{i}N/n_{i}=1}
. Then, one solution to the system of simultaneous congruences is: and the minimal solution,
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Chinese remainder theorem step by step in the ZX Spectrum Basic programming language
Source code in the zx programming language
10 DIM n(3): DIM a(3)
20 FOR i=1 TO 3
30 READ n(i),a(i)
40 NEXT i
50 DATA 3,2,5,3,7,2
100 LET prod=1: LET sum=0
110 FOR i=1 TO 3: LET prod=prod*n(i): NEXT i
120 FOR i=1 TO 3
130 LET p=INT (prod/n(i)): LET a=p: LET b=n(i)
140 GO SUB 1000
150 LET sum=sum+a(i)*x1*p
160 NEXT i
170 PRINT FN m(sum,prod)
180 STOP
200 DEF FN m(a,b)=a-INT (a/b)*b: REM Modulus function
1000 LET b0=b: LET x0=0: LET x1=1
1010 IF b=1 THEN RETURN
1020 IF a<=1 THEN GO TO 1100
1030 LET q=INT (a/b)
1040 LET t=b: LET b=FN m(a,b): LET a=t
1050 LET t=x0: LET x0=x1-q*x0: LET x1=t
1060 GO TO 1020
1100 IF x1<0 THEN LET x1=x1+b0
1110 RETURN
You may also check:How to resolve the algorithm Gotchas step by step in the Z80 Assembly programming language
You may also check:How to resolve the algorithm Map range step by step in the Maple programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the PHP programming language
You may also check:How to resolve the algorithm Object serialization step by step in the J programming language
You may also check:How to resolve the algorithm Approximate equality step by step in the Common Lisp programming language