From 980947cf79a057f775b50233e1080f7b2f2cbb44 Mon Sep 17 00:00:00 2001 From: sundaram123krishnan Date: Thu, 4 May 2023 21:49:23 +0530 Subject: [PATCH 1/8] Added Miller-Rabin primality test --- Maths/MillerRabinPrimaliyTest.js | 59 ++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Maths/MillerRabinPrimaliyTest.js diff --git a/Maths/MillerRabinPrimaliyTest.js b/Maths/MillerRabinPrimaliyTest.js new file mode 100644 index 0000000000..447b2cbcdd --- /dev/null +++ b/Maths/MillerRabinPrimaliyTest.js @@ -0,0 +1,59 @@ + + + + +const power = (base, exponent, modulus) => { + let result = 1 + base %= modulus + + while (exponent) { + if (exponent & 1) { + result = result * base % modulus + } + base = base * base % modulus + exponent >>= 1 + } + return result +} + +const checkComposite = (n, a, d, s) => { + let x = power(a, d, n) + + if (x == 1 || x == n - 1) { + return false + } + + for (let r = 1; r < s; r++) { + x = x * x % n + if (x == n - 1) { + return false + } + } + return true +} + +const MillerRabin = (n, iter = 5) => { + if (n < 4) { + return n == 2 || n == 3 + } + + let s = 0 + + let d = n - 1 + + while ((d & 1)==0) { + d >>= 1 + s++ + } + + for (let i = 0; i < iter; i++) { + let a = 2 + Math.floor(Math.random() * (n-2)) % (n - 4); + if (checkComposite(n, a, d, s)) { + return false + } + } + return true +} + + +console.log(MillerRabin(123)) From 8a0861d956292ee6edd4c9ced244b4d50b9cdd34 Mon Sep 17 00:00:00 2001 From: sundaram123krishnan Date: Thu, 4 May 2023 21:59:43 +0530 Subject: [PATCH 2/8] Modified Miler Rabin Primality test --- Maths/MillerRabinPrimalityTest.js | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Maths/MillerRabinPrimalityTest.js diff --git a/Maths/MillerRabinPrimalityTest.js b/Maths/MillerRabinPrimalityTest.js new file mode 100644 index 0000000000..d2c2097e1e --- /dev/null +++ b/Maths/MillerRabinPrimalityTest.js @@ -0,0 +1,69 @@ +/* + * Author: Sundaram krishnan (https://github.com/sundaram123krishnan) + * Miller–Rabin primality test : https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#:~:text=The%20Miller%E2%80%93Rabin%20primality%20test,the%20Solovay%E2%80%93Strassen%20primality%20test. + * Used to check whether given number is probably prime or not + * return true if the number is probably a prime + * else false + */ + +/** + * @function MillerRabin + * @description -> Checking if a number is probably prime or not + * @param {number} number + * @returns {boolean} true if the number is prime, otherwise false + */ + +const power = (base, exponent, modulus) => { + let result = 1 + base %= modulus + + while (exponent) { + if (exponent & 1) { + result = (result * base) % modulus + } + base = (base * base) % modulus + exponent >>= 1 + } + return result + } + + const checkComposite = (n, a, d, s) => { + let x = power(a, d, n) + + if (x == 1 || x == n - 1) { + return false + } + + + for (let r = 1; r < s; r++) { + x = (x * x) % n + if (x == n - 1) { + return false + } + } + return true + } + + const MillerRabin = (n, iter = 5) => { + if (n < 4) { + return n == 2 || n == 3 + } + + let s = 0 + + let d = n - 1 + + while ((d & 1) == 0) { + d >>= 1 + s++ + } + + for (let i = 0; i < iter; i++) { + let a = 2 + (Math.floor(Math.random() * (n - 2)) % (n - 4)) + if (checkComposite(n, a, d, s)) { + return false + } + } + return true + } + \ No newline at end of file From 0cd6edca9af74812425676bf11850b626f2e6d39 Mon Sep 17 00:00:00 2001 From: Sundaram krishnan <104441812+sundaram123krishnan@users.noreply.github.com> Date: Thu, 4 May 2023 22:04:40 +0530 Subject: [PATCH 3/8] Delete MillerRabinPrimaliyTest.js --- Maths/MillerRabinPrimaliyTest.js | 59 -------------------------------- 1 file changed, 59 deletions(-) delete mode 100644 Maths/MillerRabinPrimaliyTest.js diff --git a/Maths/MillerRabinPrimaliyTest.js b/Maths/MillerRabinPrimaliyTest.js deleted file mode 100644 index 447b2cbcdd..0000000000 --- a/Maths/MillerRabinPrimaliyTest.js +++ /dev/null @@ -1,59 +0,0 @@ - - - - -const power = (base, exponent, modulus) => { - let result = 1 - base %= modulus - - while (exponent) { - if (exponent & 1) { - result = result * base % modulus - } - base = base * base % modulus - exponent >>= 1 - } - return result -} - -const checkComposite = (n, a, d, s) => { - let x = power(a, d, n) - - if (x == 1 || x == n - 1) { - return false - } - - for (let r = 1; r < s; r++) { - x = x * x % n - if (x == n - 1) { - return false - } - } - return true -} - -const MillerRabin = (n, iter = 5) => { - if (n < 4) { - return n == 2 || n == 3 - } - - let s = 0 - - let d = n - 1 - - while ((d & 1)==0) { - d >>= 1 - s++ - } - - for (let i = 0; i < iter; i++) { - let a = 2 + Math.floor(Math.random() * (n-2)) % (n - 4); - if (checkComposite(n, a, d, s)) { - return false - } - } - return true -} - - -console.log(MillerRabin(123)) From 4f38f0bf8aeac75433a30be1b6fccd5ac0d6f06a Mon Sep 17 00:00:00 2001 From: sundaram123krishnan Date: Thu, 4 May 2023 22:24:44 +0530 Subject: [PATCH 4/8] added npm format --- package-lock.json | 16 +++++++++++++++- package.json | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2c86fde142..255bc16a74 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,8 @@ "dependencies": { "@babel/core": "^7.19.3", "@babel/plugin-transform-runtime": "^7.19.1", - "@babel/preset-env": "^7.19.4" + "@babel/preset-env": "^7.19.4", + "format": "^0.2.2" }, "devDependencies": { "@babel/eslint-parser": "^7.19.1", @@ -4399,6 +4400,14 @@ "integrity": "sha512-8/sOawo8tJ4QOBX8YlQBMxL8+RLZfxMQOif9o0KUKTNTjMYElWPE0r/m5VNFxTRd0NSw8qSy8dajrwX4RYI1Hw==", "dev": true }, + "node_modules/format": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/format/-/format-0.2.2.tgz", + "integrity": "sha512-wzsgA6WOq+09wrU1tsJ09udeR/YZRaeArL9e1wPbFg3GG2yDnC2ldKpxs4xunpFF9DgqCqOIra3bc1HWrJ37Ww==", + "engines": { + "node": ">=0.4.x" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -11560,6 +11569,11 @@ "integrity": "sha512-8/sOawo8tJ4QOBX8YlQBMxL8+RLZfxMQOif9o0KUKTNTjMYElWPE0r/m5VNFxTRd0NSw8qSy8dajrwX4RYI1Hw==", "dev": true }, + "format": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/format/-/format-0.2.2.tgz", + "integrity": "sha512-wzsgA6WOq+09wrU1tsJ09udeR/YZRaeArL9e1wPbFg3GG2yDnC2ldKpxs4xunpFF9DgqCqOIra3bc1HWrJ37Ww==" + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", diff --git a/package.json b/package.json index 6ab94ac09e..bcf27eefb8 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "dependencies": { "@babel/core": "^7.19.3", "@babel/plugin-transform-runtime": "^7.19.1", - "@babel/preset-env": "^7.19.4" + "@babel/preset-env": "^7.19.4", + "format": "^0.2.2" }, "devDependencies": { "@babel/eslint-parser": "^7.19.1", From 8d922c7563935f8fc7803856d02e74f12e2ef9e8 Mon Sep 17 00:00:00 2001 From: sundaram123krishnan Date: Thu, 4 May 2023 22:26:45 +0530 Subject: [PATCH 5/8] modified with proper indentaion and code quality --- ...lityTest.js => MilerRabinPrimalityTest.js} | 66 +++++++++---------- 1 file changed, 32 insertions(+), 34 deletions(-) rename Maths/{MillerRabinPrimalityTest.js => MilerRabinPrimalityTest.js} (65%) diff --git a/Maths/MillerRabinPrimalityTest.js b/Maths/MilerRabinPrimalityTest.js similarity index 65% rename from Maths/MillerRabinPrimalityTest.js rename to Maths/MilerRabinPrimalityTest.js index d2c2097e1e..8432083769 100644 --- a/Maths/MillerRabinPrimalityTest.js +++ b/Maths/MilerRabinPrimalityTest.js @@ -12,58 +12,56 @@ * @param {number} number * @returns {boolean} true if the number is prime, otherwise false */ - const power = (base, exponent, modulus) => { let result = 1 base %= modulus - + while (exponent) { - if (exponent & 1) { - result = (result * base) % modulus - } - base = (base * base) % modulus - exponent >>= 1 + if (exponent & 1) { + result = (result * base) % modulus + } + base = (base * base) % modulus + exponent >>= 1 } return result - } - - const checkComposite = (n, a, d, s) => { +} + +const checkComposite = (n, a, d, s) => { let x = power(a, d, n) - + if (x == 1 || x == n - 1) { - return false + return false } - - + + for (let r = 1; r < s; r++) { - x = (x * x) % n - if (x == n - 1) { - return false - } + x = (x * x) % n + if (x == n - 1) { + return false + } } return true - } - - const MillerRabin = (n, iter = 5) => { +} + +const MillerRabin = (n, iter = 5) => { if (n < 4) { - return n == 2 || n == 3 + return n == 2 || n == 3 } - + let s = 0 - + let d = n - 1 - + while ((d & 1) == 0) { - d >>= 1 - s++ + d >>= 1 + s++ } - + for (let i = 0; i < iter; i++) { - let a = 2 + (Math.floor(Math.random() * (n - 2)) % (n - 4)) - if (checkComposite(n, a, d, s)) { - return false - } + let a = 2 + (Math.floor(Math.random() * (n - 2)) % (n - 4)) + if (checkComposite(n, a, d, s)) { + return false + } } return true - } - \ No newline at end of file +} \ No newline at end of file From cdc62830f90e47c121877b2d464b59642089980a Mon Sep 17 00:00:00 2001 From: sundaram123krishnan Date: Fri, 5 May 2023 12:29:51 +0530 Subject: [PATCH 6/8] formatted using standard js --- .vscode/tasks.json | 16 +++++++ Maths/MilerRabinPrimalityTest.js | 74 ++++++++++++++++---------------- 2 files changed, 54 insertions(+), 36 deletions(-) create mode 100644 .vscode/tasks.json diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000000..68f846b1c9 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,16 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "npm", + "script": "test", + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": [], + "label": "npm: test", + "detail": "jest" + } + ] +} \ No newline at end of file diff --git a/Maths/MilerRabinPrimalityTest.js b/Maths/MilerRabinPrimalityTest.js index 8432083769..aef0355418 100644 --- a/Maths/MilerRabinPrimalityTest.js +++ b/Maths/MilerRabinPrimalityTest.js @@ -12,56 +12,58 @@ * @param {number} number * @returns {boolean} true if the number is prime, otherwise false */ + const power = (base, exponent, modulus) => { - let result = 1 - base %= modulus + let result = 1 + base %= modulus - while (exponent) { - if (exponent & 1) { - result = (result * base) % modulus - } - base = (base * base) % modulus - exponent >>= 1 + while (exponent) { + if (exponent & 1) { + result = (result * base) % modulus } - return result + base = (base * base) % modulus + exponent >>= 1 + } + return result } const checkComposite = (n, a, d, s) => { - let x = power(a, d, n) - - if (x == 1 || x == n - 1) { - return false - } + let x = power(a, d, n) + if (x === 1 || x === n - 1) { + return false + } - for (let r = 1; r < s; r++) { - x = (x * x) % n - if (x == n - 1) { - return false - } + for (let r = 1; r < s; r++) { + x = (x * x) % n + if (x === n - 1) { + return false } - return true + } + return true } const MillerRabin = (n, iter = 5) => { - if (n < 4) { - return n == 2 || n == 3 - } + if (n < 4) { + return n === 2 || n === 3 + } - let s = 0 + let s = 0 - let d = n - 1 + let d = n - 1 - while ((d & 1) == 0) { - d >>= 1 - s++ - } + while ((d & 1) === 0) { + d >>= 1 + s++ + } - for (let i = 0; i < iter; i++) { - let a = 2 + (Math.floor(Math.random() * (n - 2)) % (n - 4)) - if (checkComposite(n, a, d, s)) { - return false - } + for (let i = 0; i < iter; i++) { + const a = 2 + (Math.floor(Math.random() * (n - 2)) % (n - 4)) + if (checkComposite(n, a, d, s)) { + return false } - return true -} \ No newline at end of file + } + return true +} + +export { MillerRabin } From da3540ac6f20c92da240d5cd48616d5757632778 Mon Sep 17 00:00:00 2001 From: sundaram123krishnan Date: Fri, 5 May 2023 16:10:20 +0530 Subject: [PATCH 7/8] deleted tasks.json --- .vscode/tasks.json | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 .vscode/tasks.json diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 68f846b1c9..0000000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "type": "npm", - "script": "test", - "group": { - "kind": "build", - "isDefault": true - }, - "problemMatcher": [], - "label": "npm: test", - "detail": "jest" - } - ] -} \ No newline at end of file From a8df076852edf93a628aa886a03374fd50c9512e Mon Sep 17 00:00:00 2001 From: sundaram123krishnan Date: Sat, 6 May 2023 22:06:24 +0530 Subject: [PATCH 8/8] uninstalled npm format --- package-lock.json | 16 +--------------- package.json | 3 +-- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/package-lock.json b/package-lock.json index 255bc16a74..2c86fde142 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,8 +11,7 @@ "dependencies": { "@babel/core": "^7.19.3", "@babel/plugin-transform-runtime": "^7.19.1", - "@babel/preset-env": "^7.19.4", - "format": "^0.2.2" + "@babel/preset-env": "^7.19.4" }, "devDependencies": { "@babel/eslint-parser": "^7.19.1", @@ -4400,14 +4399,6 @@ "integrity": "sha512-8/sOawo8tJ4QOBX8YlQBMxL8+RLZfxMQOif9o0KUKTNTjMYElWPE0r/m5VNFxTRd0NSw8qSy8dajrwX4RYI1Hw==", "dev": true }, - "node_modules/format": { - "version": "0.2.2", - "resolved": "https://registry.npmjs.org/format/-/format-0.2.2.tgz", - "integrity": "sha512-wzsgA6WOq+09wrU1tsJ09udeR/YZRaeArL9e1wPbFg3GG2yDnC2ldKpxs4xunpFF9DgqCqOIra3bc1HWrJ37Ww==", - "engines": { - "node": ">=0.4.x" - } - }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -11569,11 +11560,6 @@ "integrity": "sha512-8/sOawo8tJ4QOBX8YlQBMxL8+RLZfxMQOif9o0KUKTNTjMYElWPE0r/m5VNFxTRd0NSw8qSy8dajrwX4RYI1Hw==", "dev": true }, - "format": { - "version": "0.2.2", - "resolved": "https://registry.npmjs.org/format/-/format-0.2.2.tgz", - "integrity": "sha512-wzsgA6WOq+09wrU1tsJ09udeR/YZRaeArL9e1wPbFg3GG2yDnC2ldKpxs4xunpFF9DgqCqOIra3bc1HWrJ37Ww==" - }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", diff --git a/package.json b/package.json index bcf27eefb8..6ab94ac09e 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,7 @@ "dependencies": { "@babel/core": "^7.19.3", "@babel/plugin-transform-runtime": "^7.19.1", - "@babel/preset-env": "^7.19.4", - "format": "^0.2.2" + "@babel/preset-env": "^7.19.4" }, "devDependencies": { "@babel/eslint-parser": "^7.19.1", pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy