The object of considerations is linear congruence in form:
where:
Sometimes it is useful to limit the result set by given boundary, so we can find general solution or limited set that:
Congruence has a soltution
The most common algorithmic approach to solve this exercise is to find the modular multiplicative inverse
To achieve this we can use the Extended Euclidian Algorithm to compute Bézout coefficients of Bézout's identity (linear Diophantine equation):
which is particularly useful when
in sense of symbols used in Bézout's identity.
We aim to achieve the form of linear congruence equasion where
Next step in our computations will be verify the existence of the solution. If solution exist we can compute the modular multiplicative inverse using Extended Euclidian Algorithm:
and multiply equasion by
In general solution of linear congruence equasion is the infinite set:
But we will be looking for solution set
where
Below simple schematic is presented. In general solutions we can assume that
This package is intended for all people who want to solve the equasion in
npm install @scitia/lincoln
Requires at least TypeScript: 5.6.3
First thing you have to do is to create object of main Lincoln
class depending on your package.json type property (commonjs, module).
import { Lincoln } from '@scitia/lincoln';
const lin = new Lincoln();
or:
const lincoln = require('@scitia/lincoln');
const lin = new lincoln.Lincoln();
In every case returned data has structure as type below:
export type SolverResult = {
ok: boolean;
mode: string;
explanation: string | undefined;
message: string | undefined;
result: number[] | number | undefined;
};
Result of calculations are returned in result
field.
Looking for solution is very simple. We have to create object of Lincoln class, call a method construct()
or each setter for required parameters: a
, b
, n
according to math preface.
lin.construct(701, 529, 7); // a, b, n
const solution = lin.general(); //array of two numbers [first solution, minimal modulus]
Sometimes it may be more convenient for you to call each setter method:
lin.slope(701);
lin.congruent(529);
lin.modulus(7);
const solution = lin.general() //array of two numbers [first solution, minimal modulus]
If you need all solutions limited to given boundary you can use atBoundary()
solver method instead of general()
:
const solution = lin.atBoundary(20); // [4, 11, 18]
In another case maybe you will need to know first N solutions. Than you can use first()
method:
const solution = lin.first(4); // [4, 11, 18, 25]
If you want to know only first solution you just need to call the method firstOnly()
:
const solution = lin.firstOnly(); // 4
Other use case is when you need dynamically modify left or right linear congruence equasion side by adding or substracting values. Here comes four methods that allow you to achieve this directly lpus()
, lminus()
, rplus()
, rminus()
.
// 3x≡8 (mod 34) original form
lin.construct(3, 8, 34);
lin.lminus(4); // 3x-4≡8 (mod 34)
lin.lplus(1); // 3x-4+1≡8 (mod 34)
lin.rplus(6, 5) // 3x-4+1≡8+6+5 (mod 34)
lin.rminus(9); // 3x-4+1≡8+6+5-9 (mod 34)
// 3x≡13 (mod 34)
const solution = lin.firstOnly(); // 27
This small library provides functionalities to calculate solution for linear congruence equasions. Please report any problems or bugs you notice. If you notice any problems please report it as an issue or give feedback if you want to extend this small library with another more advanced topics 😃