Skip to content

Commit 34bd82f

Browse files
authored
Merge pull request technoph1le#179 from JanluOfficial/main
[Snippets] Added several new mathematical snippets for JavaScript.
2 parents 57cfb36 + a440b5a commit 34bd82f

File tree

10 files changed

+305
-2
lines changed

10 files changed

+305
-2
lines changed

public/consolidated/javascript.json

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,16 +400,109 @@
400400
{
401401
"name": "Mathematical Functions",
402402
"snippets": [
403+
{
404+
"title": "Combinations",
405+
"description": "Calculates the number of combinations (denoted as C(n,r) or \"n choose r\"), which determines how many ways you can select r items from n items without considering the order.",
406+
"author": "JanluOfficial",
407+
"tags": [
408+
"math",
409+
"number-theory",
410+
"algebra"
411+
],
412+
"contributors": [],
413+
"code": "function combinations(n, r) {\n function factorial(x) {\n if (x === 0 || x === 1) return 1;\n let result = 1;\n for (let i = 2; i <= x; i++) {\n result *= i;\n }\n return result;\n }\n return factorial(n) / (factorial(r) * factorial(n - r));\n}\n\n// Usage:\ncombinations(12,24); // Returns: 7.720248753351544e-16\ncombinations(1,22); // Returns: 8.896791392450574e-22\n"
414+
},
415+
{
416+
"title": "Cross Product",
417+
"description": "Computes the cross product of two 3D vectors, which results in a vector perpendicular to both.",
418+
"author": "JanluOfficial",
419+
"tags": [
420+
"math",
421+
"vector-algebra"
422+
],
423+
"contributors": [],
424+
"code": "function crossProduct(a, b) {\n if (a.length !== 3 || b.length !== 3) {\n throw new Error('Vectors must be 3-dimensional');\n }\n\n return [\n a[1] * b[2] - a[2] * b[1],\n a[2] * b[0] - a[0] * b[2],\n a[0] * b[1] - a[1] * b[0]\n ];\n}\n\n// Usage:\ncrossProduct([1, 2, 3], [4, 5, 6]); // Returns: [-3, 6, -3] \n"
425+
},
426+
{
427+
"title": "Dot Product",
428+
"description": "Computes the dot product of two vectors, which is the sum of the products of corresponding elements.",
429+
"author": "JanluOfficial",
430+
"tags": [
431+
"math",
432+
"vector-algebra"
433+
],
434+
"contributors": [],
435+
"code": "function dotProduct(a, b) {\n if (a.length !== b.length) {\n throw new Error('Vectors must be of the same length');\n }\n\n return a.reduce((sum, value, index) => sum + value * b[index], 0);\n}\n\n// Usage:\ndotProduct([1, 2, 3], [4, 5, 6]); // Returns: 32\n"
436+
},
437+
{
438+
"title": "Error function",
439+
"description": "Computes the error function (erf(x)) for a given input x, which is a mathematical function used frequently in probability, statistics, and partial differential equations.",
440+
"author": "JanluOfficial",
441+
"tags": [
442+
"math"
443+
],
444+
"contributors": [],
445+
"code": "function erf(x) {\n const sign = Math.sign(x);\n const absX = Math.abs(x);\n const t = 1 / (1 + 0.3275911 * absX);\n const a1 = 0.254829592, a2 = -0.284496736, a3 = 1.421413741, a4 = -1.453152027, a5 = 1.061405429;\n const poly = t * (a1 + t * (a2 + t * (a3 + t * (a4 + t * a5))));\n return sign * (1 - poly * Math.exp(-absX * absX));\n}\n\n// Usage:\nerf(-1); // Returns: -0.8427006897475899\nerf(1); // Returns: 0.8427006897475899\n"
446+
},
403447
{
404448
"title": "Greatest Common Divisor",
405449
"description": "Calculates the largest positive integer that divides each of the integers without leaving a remainder. Useful for calculating aspect ratios.",
406450
"author": "JanluOfficial",
407451
"tags": [
408452
"math",
409-
"division"
453+
"division",
454+
"number-theory",
455+
"algebra"
410456
],
411457
"contributors": [],
412458
"code": "function gcd(a, b) {\n while (b !== 0) {\n let temp = b;\n b = a % b;\n a = temp;\n }\n return a;\n}\n\n// Usage:\ngcd(1920, 1080); // Returns: 120\ngcd(1920, 1200); // Returns: 240\ngcd(5,12); // Returns: 1\n"
459+
},
460+
{
461+
"title": "Least common multiple",
462+
"description": "Computes the least common multiple (LCM) of two numbers 𝑎 and b. The LCM is the smallest positive integer that is divisible by both a and b.",
463+
"author": "JanluOfficial",
464+
"tags": [
465+
"math",
466+
"number-theory",
467+
"algebra"
468+
],
469+
"contributors": [],
470+
"code": "function lcm(a, b) {\n function gcd(x, y) {\n while (y !== 0) {\n const temp = y;\n y = x % y;\n x = temp;\n }\n return Math.abs(x);\n }\n return Math.abs(a * b) / gcd(a, b);\n}\n\n// Usage:\nlcm(12,16); // Returns: 48\nlcm(8,20); // Returns: 40\nlcm(16,17); // Returns: 272\n"
471+
},
472+
{
473+
"title": "Matrix Multiplication",
474+
"description": "Multiplies two matrices, where the number of columns in the first matrix equals the number of rows in the second.",
475+
"author": "JanluOfficial",
476+
"tags": [
477+
"math",
478+
"matrix-algebra"
479+
],
480+
"contributors": [],
481+
"code": "function matrixMultiply(A, B) {\n const rowsA = A.length;\n const colsA = A[0].length;\n const rowsB = B.length;\n const colsB = B[0].length;\n\n if (colsA !== rowsB) {\n throw new Error('Number of columns of A must equal the number of rows of B');\n }\n\n let result = Array.from({ length: rowsA }, () => Array(colsB).fill(0));\n\n for (let i = 0; i < rowsA; i++) {\n for (let j = 0; j < colsB; j++) {\n for (let k = 0; k < colsA; k++) {\n result[i][j] += A[i][k] * B[k][j];\n }\n }\n }\n\n return result;\n}\n\n// Usage:\nmatrixMultiply([[1, 2], [3, 4]], [[5, 6], [7, 8]]); // Returns: [[19, 22], [43, 50]]\n"
482+
},
483+
{
484+
"title": "Modular Inverse",
485+
"description": "Computes the modular multiplicative inverse of a number a under modulo m, which is the integer x such that (a*x) mod m=1.",
486+
"author": "JanluOfficial",
487+
"tags": [
488+
"math",
489+
"number-theory",
490+
"algebra"
491+
],
492+
"contributors": [],
493+
"code": "function modInverse(a, m) {\n function extendedGCD(a, b) {\n if (b === 0) {\n return { gcd: a, x: 1, y: 0 };\n }\n const { gcd, x: x1, y: y1 } = extendedGCD(b, a % b);\n const x = y1;\n const y = x1 - Math.floor(a / b) * y1;\n return { gcd, x, y };\n }\n\n const { gcd, x } = extendedGCD(a, m);\n\n if (gcd !== 1) {\n return null;\n }\n\n return (x % m + m) % m;\n}\n\n// Usage:\nmodInverse(3, 26); // Returns: 9\nmodInverse(10, 17); // Returns: 12\nmodInverse(6, 9); // Returns: null\n"
494+
},
495+
{
496+
"title": "Prime Number",
497+
"description": "Checks if a number is a prime number or not.",
498+
"author": "JanluOfficial",
499+
"tags": [
500+
"math",
501+
"number-theory",
502+
"algebra"
503+
],
504+
"contributors": [],
505+
"code": "function isPrime(num) {\n if (num <= 1) return false; // 0 and 1 are not prime numbers\n if (num <= 3) return true; // 2 and 3 are prime numbers\n if (num % 2 === 0 || num % 3 === 0) return false; // Exclude multiples of 2 and 3\n\n // Check divisors from 5 to √num, skipping multiples of 2 and 3\n for (let i = 5; i * i <= num; i += 6) {\n if (num % i === 0 || num % (i + 2) === 0) return false;\n }\n return true;\n}\n\n// Usage:\nisPrime(69); // Returns: false\nisPrime(17); // Returns: true\n"
413506
}
414507
]
415508
},
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
title: Combinations
3+
description: Calculates the number of combinations (denoted as C(n,r) or "n choose r"), which determines how many ways you can select r items from n items without considering the order.
4+
author: JanluOfficial
5+
tags: math,number-theory,algebra
6+
---
7+
8+
```js
9+
function combinations(n, r) {
10+
if (n < 0 || r < 0 || n < r) {
11+
throw new Error('Invalid input: n and r must be non-negative and n must be greater than or equal to r.');
12+
}
13+
14+
function factorial(x) {
15+
if (x === 0 || x === 1) return 1;
16+
let result = 1;
17+
for (let i = 2; i <= x; i++) {
18+
result *= i;
19+
}
20+
return result;
21+
}
22+
23+
const numerator = factorial(n);
24+
const denominator = factorial(r) * factorial(n - r);
25+
return numerator / denominator;
26+
}
27+
28+
// Usage:
29+
combinations(24,22); // Returns: 276
30+
combinations(5,3); // Returns: 10
31+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Cross Product
3+
description: Computes the cross product of two 3D vectors, which results in a vector perpendicular to both.
4+
author: JanluOfficial
5+
tags: math,vector-algebra
6+
---
7+
8+
```js
9+
function crossProduct(a, b) {
10+
if (a.length !== 3 || b.length !== 3) {
11+
throw new Error('Vectors must be 3-dimensional');
12+
}
13+
14+
return [
15+
a[1] * b[2] - a[2] * b[1],
16+
a[2] * b[0] - a[0] * b[2],
17+
a[0] * b[1] - a[1] * b[0]
18+
];
19+
}
20+
21+
// Usage:
22+
crossProduct([1, 2, 3], [4, 5, 6]); // Returns: [-3, 6, -3]
23+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Dot Product
3+
description: Computes the dot product of two vectors, which is the sum of the products of corresponding elements.
4+
author: JanluOfficial
5+
tags: math,vector-algebra
6+
---
7+
8+
```js
9+
function dotProduct(a, b) {
10+
if (a.length !== b.length) {
11+
throw new Error('Vectors must be of the same length');
12+
}
13+
14+
return a.reduce((sum, value, index) => sum + value * b[index], 0);
15+
}
16+
17+
// Usage:
18+
dotProduct([1, 2, 3], [4, 5, 6]); // Returns: 32
19+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Error function
3+
description: Computes the error function (erf(x)) for a given input x, which is a mathematical function used frequently in probability, statistics, and partial differential equations.
4+
author: JanluOfficial
5+
tags: math
6+
---
7+
8+
```js
9+
function erf(x) {
10+
const sign = Math.sign(x);
11+
const absX = Math.abs(x);
12+
const t = 1 / (1 + 0.3275911 * absX);
13+
const a1 = 0.254829592, a2 = -0.284496736, a3 = 1.421413741, a4 = -1.453152027, a5 = 1.061405429;
14+
const poly = t * (a1 + t * (a2 + t * (a3 + t * (a4 + t * a5))));
15+
return sign * (1 - poly * Math.exp(-absX * absX));
16+
}
17+
18+
// Usage:
19+
erf(-1); // Returns: -0.8427006897475899
20+
erf(1); // Returns: 0.8427006897475899
21+
```

snippets/javascript/mathematical-functions/greatest-common-divisor.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Greatest Common Divisor
33
description: Calculates the largest positive integer that divides each of the integers without leaving a remainder. Useful for calculating aspect ratios.
44
author: JanluOfficial
5-
tags: math,division
5+
tags: math,division,number-theory,algebra
66
---
77

88
```js
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Least common multiple
3+
description: Computes the least common multiple (LCM) of two numbers 𝑎 and b. The LCM is the smallest positive integer that is divisible by both a and b.
4+
author: JanluOfficial
5+
tags: math,number-theory,algebra
6+
---
7+
8+
```js
9+
function lcm(a, b) {
10+
function gcd(x, y) {
11+
while (y !== 0) {
12+
const temp = y;
13+
y = x % y;
14+
x = temp;
15+
}
16+
return Math.abs(x);
17+
}
18+
return Math.abs(a * b) / gcd(a, b);
19+
}
20+
21+
// Usage:
22+
lcm(12,16); // Returns: 48
23+
lcm(8,20); // Returns: 40
24+
lcm(16,17); // Returns: 272
25+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: Matrix Multiplication
3+
description: Multiplies two matrices, where the number of columns in the first matrix equals the number of rows in the second.
4+
author: JanluOfficial
5+
tags: math,matrix-algebra
6+
---
7+
8+
```js
9+
function matrixMultiply(A, B) {
10+
const rowsA = A.length;
11+
const colsA = A[0].length;
12+
const rowsB = B.length;
13+
const colsB = B[0].length;
14+
15+
if (colsA !== rowsB) {
16+
throw new Error('Number of columns of A must equal the number of rows of B');
17+
}
18+
19+
let result = Array.from({ length: rowsA }, () => Array(colsB).fill(0));
20+
21+
for (let i = 0; i < rowsA; i++) {
22+
for (let j = 0; j < colsB; j++) {
23+
for (let k = 0; k < colsA; k++) {
24+
result[i][j] += A[i][k] * B[k][j];
25+
}
26+
}
27+
}
28+
29+
return result;
30+
}
31+
32+
// Usage:
33+
matrixMultiply([[1, 2], [3, 4]], [[5, 6], [7, 8]]); // Returns: [[19, 22], [43, 50]]
34+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: Modular Inverse
3+
description: Computes the modular multiplicative inverse of a number a under modulo m, which is the integer x such that (a*x) mod m=1.
4+
author: JanluOfficial
5+
tags: math,number-theory,algebra
6+
---
7+
8+
```js
9+
function modInverse(a, m) {
10+
function extendedGCD(a, b) {
11+
if (b === 0) {
12+
return { gcd: a, x: 1, y: 0 };
13+
}
14+
const { gcd, x: x1, y: y1 } = extendedGCD(b, a % b);
15+
const x = y1;
16+
const y = x1 - Math.floor(a / b) * y1;
17+
return { gcd, x, y };
18+
}
19+
20+
const { gcd, x } = extendedGCD(a, m);
21+
22+
if (gcd !== 1) {
23+
return null;
24+
}
25+
26+
return (x % m + m) % m;
27+
}
28+
29+
// Usage:
30+
modInverse(3, 26); // Returns: 9
31+
modInverse(10, 17); // Returns: 12
32+
modInverse(6, 9); // Returns: null
33+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Prime Number
3+
description: Checks if a number is a prime number or not.
4+
author: JanluOfficial
5+
tags: math,number-theory,algebra
6+
---
7+
8+
```js
9+
function isPrime(num) {
10+
if (num <= 1) return false; // 0 and 1 are not prime numbers
11+
if (num <= 3) return true; // 2 and 3 are prime numbers
12+
if (num % 2 === 0 || num % 3 === 0) return false; // Exclude multiples of 2 and 3
13+
14+
// Check divisors from 5 to √num, skipping multiples of 2 and 3
15+
for (let i = 5; i * i <= num; i += 6) {
16+
if (num % i === 0 || num % (i + 2) === 0) return false;
17+
}
18+
return true;
19+
}
20+
21+
// Usage:
22+
isPrime(69); // Returns: false
23+
isPrime(17); // Returns: true
24+
```

0 commit comments

Comments
 (0)