Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9d0dc84

Browse files
A bunch of JS math snippets
1 parent 65140cc commit 9d0dc84

File tree

10 files changed

+298
-2
lines changed

10 files changed

+298
-2
lines changed

‎public/consolidated/javascript.json‎

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,16 +367,109 @@
367367
{
368368
"categoryName": "Mathematical Functions",
369369
"snippets": [
370+
{
371+
"title": "Combinations",
372+
"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.",
373+
"author": "JanluOfficial",
374+
"tags": [
375+
"math",
376+
"number-theory",
377+
"algebra"
378+
],
379+
"contributors": [],
380+
"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"
381+
},
382+
{
383+
"title": "Cross Product",
384+
"description": "Computes the cross product of two 3D vectors, which results in a vector perpendicular to both.",
385+
"author": "JanluOfficial",
386+
"tags": [
387+
"math",
388+
"vector-algebra"
389+
],
390+
"contributors": [],
391+
"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"
392+
},
393+
{
394+
"title": "Dot Product",
395+
"description": "Computes the dot product of two vectors, which is the sum of the products of corresponding elements.",
396+
"author": "JanluOfficial",
397+
"tags": [
398+
"math",
399+
"vector-algebra"
400+
],
401+
"contributors": [],
402+
"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"
403+
},
404+
{
405+
"title": "Error function",
406+
"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.",
407+
"author": "JanluOfficial",
408+
"tags": [
409+
"math"
410+
],
411+
"contributors": [],
412+
"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"
413+
},
370414
{
371415
"title": "Greatest Common Divisor",
372416
"description": "Calculates the largest positive integer that divides each of the integers without leaving a remainder. Useful for calculating aspect ratios.",
373417
"author": "JanluOfficial",
374418
"tags": [
375419
"math",
376-
"division"
420+
"division",
421+
"number-theory",
422+
"algebra"
377423
],
378424
"contributors": [],
379425
"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"
426+
},
427+
{
428+
"title": "Least common multiple",
429+
"description": "Computes the least common multiple (LCM) of two numbers a and b. The LCM is the smallest positive integer that is divisible by both a and b.",
430+
"author": "JanluOfficial",
431+
"tags": [
432+
"math",
433+
"number-theory",
434+
"algebra"
435+
],
436+
"contributors": [],
437+
"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"
438+
},
439+
{
440+
"title": "Matrix Multiplication",
441+
"description": "Multiplies two matrices, where the number of columns in the first matrix equals the number of rows in the second.",
442+
"author": "JanluOfficial",
443+
"tags": [
444+
"math",
445+
"matrix-algebra"
446+
],
447+
"contributors": [],
448+
"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"
449+
},
450+
{
451+
"title": "Modular Inverse",
452+
"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.",
453+
"author": "JanluOfficial",
454+
"tags": [
455+
"math",
456+
"number-theory",
457+
"algebra"
458+
],
459+
"contributors": [],
460+
"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"
461+
},
462+
{
463+
"title": "Prime Number",
464+
"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.",
465+
"author": "JanluOfficial",
466+
"tags": [
467+
"math",
468+
"number-theory",
469+
"algebra"
470+
],
471+
"contributors": [],
472+
"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"
380473
}
381474
]
382475
},
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
function factorial(x) {
11+
if (x === 0 || x === 1) return 1;
12+
let result = 1;
13+
for (let i = 2; i <= x; i++) {
14+
result *= i;
15+
}
16+
return result;
17+
}
18+
return factorial(n) / (factorial(r) * factorial(n - r));
19+
}
20+
21+
// Usage:
22+
combinations(12,24); // Returns: 7.720248753351544e-16
23+
combinations(1,22); // Returns: 8.896791392450574e-22
24+
```
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 a 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: 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 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)

AltStyle によって変換されたページ (->オリジナル) /