mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-08 03:45:23 +08:00
added-ModularArithmetic-code (#1217)
* added-ModularArithmetic-code * fix-typo * suggested changes
This commit is contained in:
56
Maths/ModularArithmetic.js
Normal file
56
Maths/ModularArithmetic.js
Normal file
@ -0,0 +1,56 @@
|
||||
import { extendedEuclideanGCD } from './ExtendedEuclideanGCD'
|
||||
|
||||
/**
|
||||
* https://brilliant.org/wiki/modular-arithmetic/
|
||||
* @param {Number} arg1 first argument
|
||||
* @param {Number} arg2 second argument
|
||||
* @returns {Number}
|
||||
*/
|
||||
|
||||
export class ModRing {
|
||||
constructor (MOD) {
|
||||
this.MOD = MOD
|
||||
}
|
||||
|
||||
isInputValid = (arg1, arg2) => {
|
||||
if (!this.MOD) {
|
||||
throw new Error('Modulus must be initialized in the object constructor')
|
||||
}
|
||||
if (typeof arg1 !== 'number' || typeof arg2 !== 'number') {
|
||||
throw new TypeError('Input must be Numbers')
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Modulus is Distributive property,
|
||||
* As a result, we separate it into numbers in order to keep it within MOD's range
|
||||
*/
|
||||
|
||||
add = (arg1, arg2) => {
|
||||
this.isInputValid(arg1, arg2)
|
||||
return ((arg1 % this.MOD) + (arg2 % this.MOD)) % this.MOD
|
||||
}
|
||||
|
||||
subtract = (arg1, arg2) => {
|
||||
this.isInputValid(arg1, arg2)
|
||||
// An extra MOD is added to check negative results
|
||||
return ((arg1 % this.MOD) - (arg2 % this.MOD) + this.MOD) % this.MOD
|
||||
}
|
||||
|
||||
multiply = (arg1, arg2) => {
|
||||
this.isInputValid(arg1, arg2)
|
||||
return ((arg1 % this.MOD) * (arg2 % this.MOD)) % this.MOD
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* It is not Possible to find Division directly like the above methods,
|
||||
* So we have to use the Extended Euclidean Theorem for finding Multiplicative Inverse
|
||||
* https://github.com/TheAlgorithms/JavaScript/blob/master/Maths/ExtendedEuclideanGCD.js
|
||||
*/
|
||||
|
||||
divide = (arg1, arg2) => {
|
||||
// 1st Index contains the required result
|
||||
// The theorem may have return Negative value, we need to add MOD to make it Positive
|
||||
return (extendedEuclideanGCD(arg1, arg2)[1] + this.MOD) % this.MOD
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user