Content-Length: 11090 | pFad | http://github.com/TheAlgorithms/JavaScript/pull/1748.patch
thub.com
From 7997eec3f132887079d7ba04c03d144e299de318 Mon Sep 17 00:00:00 2001
From: AmalVerma
Date: Wed, 23 Oct 2024 09:50:41 +0530
Subject: [PATCH 1/4] Added Miller Rabin for 64-bit integers
---
Maths/MillerRabin.js | 70 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 70 insertions(+)
create mode 100644 Maths/MillerRabin.js
diff --git a/Maths/MillerRabin.js b/Maths/MillerRabin.js
new file mode 100644
index 0000000000..a544b775b5
--- /dev/null
+++ b/Maths/MillerRabin.js
@@ -0,0 +1,70 @@
+/**
+* @function millerRabin
+* @description Check if number is prime or not (accurate for 64-bit integers)
+* @param {Integer} n
+* @returns {Boolean} true if prime, false otherwise
+* @url https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
+* note: Here we are using BigInt to handle large numbers
+**/
+
+// Modular Binary Exponentiation (Iterative)
+const binaryExponentiation = (base, exp, mod) => {
+ base = BigInt(base)
+ exp = BigInt(exp)
+ mod = BigInt(mod)
+
+ let result = BigInt(1)
+ base %= mod
+ while(exp){
+ if (exp & 1n){
+ result = (result * base) % mod
+ }
+ base = (base * base) % mod
+ exp = exp >> 1n
+ }
+ return result
+}
+
+// Check if number is composite
+const checkComposite = (n, a, d, s) => {
+ let x = binaryExponentiation(a, d, n)
+ if (x == 1n || x == (n - 1n)){
+ return false
+ }
+
+ for (let r = 1; r < s; r++){
+ x = (x * x) % n
+ if (x == n - 1n){
+ return false
+ }
+ }
+ return true
+}
+
+// Miller Rabin Primality Test
+export const millerRabin = (n) => {
+ n = BigInt(n)
+
+ if (n < 2){
+ return false
+ }
+
+ let s = 0n
+ let d = n - 1n
+ while((d & 1n) == 0){
+ d = d >> 1n
+ s++;
+ }
+
+ // Only first 12 primes are needed to be check to find primality of any 64-bit integer
+ let prime = [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n, 29n, 31n, 37n]
+ for(let a of prime){
+ if (n == a){
+ return true
+ }
+ if (checkComposite(n, a, d, s)){
+ return false
+ }
+ }
+ return true;
+}
From b3916f174a388db11d8af94838ffd7fafaad6e9f Mon Sep 17 00:00:00 2001
From: AmalVerma
Date: Wed, 23 Oct 2024 19:39:42 +0530
Subject: [PATCH 2/4] Formatted code
---
Maths/MillerRabin.js | 84 ++++++++++++++++++++++----------------------
1 file changed, 42 insertions(+), 42 deletions(-)
diff --git a/Maths/MillerRabin.js b/Maths/MillerRabin.js
index a544b775b5..f4c9a68ebc 100644
--- a/Maths/MillerRabin.js
+++ b/Maths/MillerRabin.js
@@ -1,70 +1,70 @@
/**
-* @function millerRabin
-* @description Check if number is prime or not (accurate for 64-bit integers)
-* @param {Integer} n
-* @returns {Boolean} true if prime, false otherwise
-* @url https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
-* note: Here we are using BigInt to handle large numbers
-**/
+ * @function millerRabin
+ * @description Check if number is prime or not (accurate for 64-bit integers)
+ * @param {Integer} n
+ * @returns {Boolean} true if prime, false otherwise
+ * @url https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
+ * note: Here we are using BigInt to handle large numbers
+ **/
// Modular Binary Exponentiation (Iterative)
const binaryExponentiation = (base, exp, mod) => {
- base = BigInt(base)
- exp = BigInt(exp)
- mod = BigInt(mod)
+ base = BigInt(base);
+ exp = BigInt(exp);
+ mod = BigInt(mod);
- let result = BigInt(1)
- base %= mod
- while(exp){
- if (exp & 1n){
- result = (result * base) % mod
+ let result = BigInt(1);
+ base %= mod;
+ while (exp) {
+ if (exp & 1n) {
+ result = (result * base) % mod;
}
- base = (base * base) % mod
- exp = exp >> 1n
+ base = (base * base) % mod;
+ exp = exp >> 1n;
}
- return result
-}
+ return result;
+};
// Check if number is composite
const checkComposite = (n, a, d, s) => {
- let x = binaryExponentiation(a, d, n)
- if (x == 1n || x == (n - 1n)){
- return false
+ let x = binaryExponentiation(a, d, n);
+ if (x == 1n || x == n - 1n) {
+ return false;
}
- for (let r = 1; r < s; r++){
- x = (x * x) % n
- if (x == n - 1n){
- return false
+ for (let r = 1; r < s; r++) {
+ x = (x * x) % n;
+ if (x == n - 1n) {
+ return false;
}
}
- return true
-}
+ return true;
+};
// Miller Rabin Primality Test
export const millerRabin = (n) => {
- n = BigInt(n)
+ n = BigInt(n);
- if (n < 2){
- return false
+ if (n < 2) {
+ return false;
}
- let s = 0n
- let d = n - 1n
- while((d & 1n) == 0){
- d = d >> 1n
+ let s = 0n;
+ let d = n - 1n;
+ while ((d & 1n) == 0) {
+ d = d >> 1n;
s++;
}
// Only first 12 primes are needed to be check to find primality of any 64-bit integer
- let prime = [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n, 29n, 31n, 37n]
- for(let a of prime){
- if (n == a){
- return true
+ let prime = [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n, 29n, 31n, 37n];
+ for (let a of prime) {
+ if (n == a) {
+ return true;
}
- if (checkComposite(n, a, d, s)){
- return false
+ if (checkComposite(n, a, d, s)) {
+ return false;
}
}
return true;
-}
+};
From 2181dfc8719a34e3bd08be8517ef73aa7f4fcf30 Mon Sep 17 00:00:00 2001
From: AmalVerma
Date: Wed, 23 Oct 2024 19:43:32 +0530
Subject: [PATCH 3/4] Revert "Formatted code"
This reverts commit b3916f174a388db11d8af94838ffd7fafaad6e9f.
---
Maths/MillerRabin.js | 84 ++++++++++++++++++++++----------------------
1 file changed, 42 insertions(+), 42 deletions(-)
diff --git a/Maths/MillerRabin.js b/Maths/MillerRabin.js
index f4c9a68ebc..a544b775b5 100644
--- a/Maths/MillerRabin.js
+++ b/Maths/MillerRabin.js
@@ -1,70 +1,70 @@
/**
- * @function millerRabin
- * @description Check if number is prime or not (accurate for 64-bit integers)
- * @param {Integer} n
- * @returns {Boolean} true if prime, false otherwise
- * @url https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
- * note: Here we are using BigInt to handle large numbers
- **/
+* @function millerRabin
+* @description Check if number is prime or not (accurate for 64-bit integers)
+* @param {Integer} n
+* @returns {Boolean} true if prime, false otherwise
+* @url https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
+* note: Here we are using BigInt to handle large numbers
+**/
// Modular Binary Exponentiation (Iterative)
const binaryExponentiation = (base, exp, mod) => {
- base = BigInt(base);
- exp = BigInt(exp);
- mod = BigInt(mod);
+ base = BigInt(base)
+ exp = BigInt(exp)
+ mod = BigInt(mod)
- let result = BigInt(1);
- base %= mod;
- while (exp) {
- if (exp & 1n) {
- result = (result * base) % mod;
+ let result = BigInt(1)
+ base %= mod
+ while(exp){
+ if (exp & 1n){
+ result = (result * base) % mod
}
- base = (base * base) % mod;
- exp = exp >> 1n;
+ base = (base * base) % mod
+ exp = exp >> 1n
}
- return result;
-};
+ return result
+}
// Check if number is composite
const checkComposite = (n, a, d, s) => {
- let x = binaryExponentiation(a, d, n);
- if (x == 1n || x == n - 1n) {
- return false;
+ let x = binaryExponentiation(a, d, n)
+ if (x == 1n || x == (n - 1n)){
+ return false
}
- for (let r = 1; r < s; r++) {
- x = (x * x) % n;
- if (x == n - 1n) {
- return false;
+ for (let r = 1; r < s; r++){
+ x = (x * x) % n
+ if (x == n - 1n){
+ return false
}
}
- return true;
-};
+ return true
+}
// Miller Rabin Primality Test
export const millerRabin = (n) => {
- n = BigInt(n);
+ n = BigInt(n)
- if (n < 2) {
- return false;
+ if (n < 2){
+ return false
}
- let s = 0n;
- let d = n - 1n;
- while ((d & 1n) == 0) {
- d = d >> 1n;
+ let s = 0n
+ let d = n - 1n
+ while((d & 1n) == 0){
+ d = d >> 1n
s++;
}
// Only first 12 primes are needed to be check to find primality of any 64-bit integer
- let prime = [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n, 29n, 31n, 37n];
- for (let a of prime) {
- if (n == a) {
- return true;
+ let prime = [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n, 29n, 31n, 37n]
+ for(let a of prime){
+ if (n == a){
+ return true
}
- if (checkComposite(n, a, d, s)) {
- return false;
+ if (checkComposite(n, a, d, s)){
+ return false
}
}
return true;
-};
+}
From dcb2a734d2cd8f4aaba3ed1aed29ff0ee3a8fb4a Mon Sep 17 00:00:00 2001
From: AmalVerma
Date: Wed, 23 Oct 2024 19:53:13 +0530
Subject: [PATCH 4/4] Formatted code
---
Maths/MillerRabin.js | 38 +++++++++++++++++++-------------------
1 file changed, 19 insertions(+), 19 deletions(-)
diff --git a/Maths/MillerRabin.js b/Maths/MillerRabin.js
index a544b775b5..638ec2d300 100644
--- a/Maths/MillerRabin.js
+++ b/Maths/MillerRabin.js
@@ -1,11 +1,11 @@
/**
-* @function millerRabin
-* @description Check if number is prime or not (accurate for 64-bit integers)
-* @param {Integer} n
-* @returns {Boolean} true if prime, false otherwise
-* @url https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
-* note: Here we are using BigInt to handle large numbers
-**/
+ * @function millerRabin
+ * @description Check if number is prime or not (accurate for 64-bit integers)
+ * @param {Integer} n
+ * @returns {Boolean} true if prime, false otherwise
+ * @url https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
+ * note: Here we are using BigInt to handle large numbers
+ **/
// Modular Binary Exponentiation (Iterative)
const binaryExponentiation = (base, exp, mod) => {
@@ -15,8 +15,8 @@ const binaryExponentiation = (base, exp, mod) => {
let result = BigInt(1)
base %= mod
- while(exp){
- if (exp & 1n){
+ while (exp) {
+ if (exp & 1n) {
result = (result * base) % mod
}
base = (base * base) % mod
@@ -28,13 +28,13 @@ const binaryExponentiation = (base, exp, mod) => {
// Check if number is composite
const checkComposite = (n, a, d, s) => {
let x = binaryExponentiation(a, d, n)
- if (x == 1n || x == (n - 1n)){
+ if (x == 1n || x == n - 1n) {
return false
}
- for (let r = 1; r < s; r++){
+ for (let r = 1; r < s; r++) {
x = (x * x) % n
- if (x == n - 1n){
+ if (x == n - 1n) {
return false
}
}
@@ -45,26 +45,26 @@ const checkComposite = (n, a, d, s) => {
export const millerRabin = (n) => {
n = BigInt(n)
- if (n < 2){
+ if (n < 2) {
return false
}
let s = 0n
let d = n - 1n
- while((d & 1n) == 0){
+ while ((d & 1n) == 0) {
d = d >> 1n
- s++;
+ s++
}
// Only first 12 primes are needed to be check to find primality of any 64-bit integer
let prime = [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n, 29n, 31n, 37n]
- for(let a of prime){
- if (n == a){
+ for (let a of prime) {
+ if (n == a) {
return true
}
- if (checkComposite(n, a, d, s)){
+ if (checkComposite(n, a, d, s)) {
return false
}
}
- return true;
+ return true
}
--- a PPN by Garber Painting Akron. With Image Size Reduction included!Fetched URL: http://github.com/TheAlgorithms/JavaScript/pull/1748.patch
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy