How to resolve the algorithm Chinese remainder theorem step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Chinese remainder theorem step by step in the Kotlin 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 Kotlin programming language

Chinese Remainder Theorem

The provided code is an implementation of the Chinese Remainder Theorem in Kotlin. The theorem provides a solution to finding a number that satisfies a set of simultaneous congruences of the form:

x ≡ a_1 (mod n_1)
x ≡ a_2 (mod n_2)
...
x ≡ a_k (mod n_k)

where the n_i's are pairwise relatively prime (i.e., they have no common factors).

Implementation Details:

  1. multInv Function:

    • This function calculates the multiplicative inverse of a modulo b, denoted as x such that (a * x) % b == 1.
    • It uses the extended Euclidean algorithm to find the inverse efficiently.
  2. chineseRemainder Function:

    • This function takes two arrays: n containing the moduli and a containing the remainders of the simultaneous congruences.
    • It calculates the product of all the moduli in n.
    • For each modulus n_i and remainder a_i, it calculates a factor p = prod / n_i.
    • It then multiplies p with a_i and the multiplicative inverse of p modulo n_i using the multInv function.
    • These products are then summed and taken modulo the overall product to find the solution x.
  3. main Function:

    • The main function demonstrates the usage of the above functions.
    • It defines two arrays n and a for a set of simultaneous congruences.
    • It calls the chineseRemainder function to find the solution x and prints the result.

Output:

For the given input:

n = [3, 5, 7]
a = [2, 3, 2]

The output will be:

23

This means that x = 23 satisfies the following congruences:

x ≡ 2 (mod 3)
x ≡ 3 (mod 5)
x ≡ 2 (mod 7)

In other words, 23 is the smallest non-negative integer that is divisible by 3 with a remainder of 2, divisible by 5 with a remainder of 3, and divisible by 7 with a remainder of 2.

Source code in the kotlin programming language

// version 1.1.2

/* returns x where (a * x) % b == 1 */
fun multInv(a: Int, b: Int): Int {
    if (b == 1) return 1
    var aa = a
    var bb = b
    var x0 = 0
    var x1 = 1
    while (aa > 1) {
        val q = aa / bb
        var t = bb
        bb = aa % bb
        aa = t
        t = x0
        x0 = x1 - q * x0
        x1 = t
    }
    if (x1 < 0) x1 += b
    return x1
} 

fun chineseRemainder(n: IntArray, a: IntArray): Int {
    val prod = n.fold(1) { acc, i -> acc * i }
    var sum = 0
    for (i in 0 until n.size) {
        val p = prod / n[i]
        sum += a[i] * multInv(p, n[i]) * p
    }
    return sum % prod
}

fun main(args: Array<String>) {
    val n = intArrayOf(3, 5, 7)
    val a = intArrayOf(2, 3, 2)
    println(chineseRemainder(n, a))
}


  

You may also check:How to resolve the algorithm Bell numbers step by step in the C# programming language
You may also check:How to resolve the algorithm Non-continuous subsequences step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Delegates step by step in the C# programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Lingo programming language
You may also check:How to resolve the algorithm Sleeping Beauty problem step by step in the Haskell programming language