Skip to content

[Snippets] Added several new mathematical snippets for JavaScript. #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 94 additions & 1 deletion public/consolidated/javascript.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,16 +400,109 @@
{
"name": "Mathematical Functions",
"snippets": [
{
"title": "Combinations",
"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.",
"author": "JanluOfficial",
"tags": [
"math",
"number-theory",
"algebra"
],
"contributors": [],
"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"
},
{
"title": "Cross Product",
"description": "Computes the cross product of two 3D vectors, which results in a vector perpendicular to both.",
"author": "JanluOfficial",
"tags": [
"math",
"vector-algebra"
],
"contributors": [],
"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"
},
{
"title": "Dot Product",
"description": "Computes the dot product of two vectors, which is the sum of the products of corresponding elements.",
"author": "JanluOfficial",
"tags": [
"math",
"vector-algebra"
],
"contributors": [],
"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"
},
{
"title": "Error function",
"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.",
"author": "JanluOfficial",
"tags": [
"math"
],
"contributors": [],
"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"
},
{
"title": "Greatest Common Divisor",
"description": "Calculates the largest positive integer that divides each of the integers without leaving a remainder. Useful for calculating aspect ratios.",
"author": "JanluOfficial",
"tags": [
"math",
"division"
"division",
"number-theory",
"algebra"
],
"contributors": [],
"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"
},
{
"title": "Least common multiple",
"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.",
"author": "JanluOfficial",
"tags": [
"math",
"number-theory",
"algebra"
],
"contributors": [],
"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"
},
{
"title": "Matrix Multiplication",
"description": "Multiplies two matrices, where the number of columns in the first matrix equals the number of rows in the second.",
"author": "JanluOfficial",
"tags": [
"math",
"matrix-algebra"
],
"contributors": [],
"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"
},
{
"title": "Modular Inverse",
"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.",
"author": "JanluOfficial",
"tags": [
"math",
"number-theory",
"algebra"
],
"contributors": [],
"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"
},
{
"title": "Prime Number",
"description": "Checks if a number is a prime number or not.",
"author": "JanluOfficial",
"tags": [
"math",
"number-theory",
"algebra"
],
"contributors": [],
"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"
}
]
},
Expand Down
31 changes: 31 additions & 0 deletions snippets/javascript/mathematical-functions/combinations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: Combinations
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.
author: JanluOfficial
tags: math,number-theory,algebra
---

```js
function combinations(n, r) {
if (n < 0 || r < 0 || n < r) {
throw new Error('Invalid input: n and r must be non-negative and n must be greater than or equal to r.');
}

function factorial(x) {
if (x === 0 || x === 1) return 1;
let result = 1;
for (let i = 2; i <= x; i++) {
result *= i;
}
return result;
}

const numerator = factorial(n);
const denominator = factorial(r) * factorial(n - r);
return numerator / denominator;
}

// Usage:
combinations(24,22); // Returns: 276
combinations(5,3); // Returns: 10
```
23 changes: 23 additions & 0 deletions snippets/javascript/mathematical-functions/cross-product.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: Cross Product
description: Computes the cross product of two 3D vectors, which results in a vector perpendicular to both.
author: JanluOfficial
tags: math,vector-algebra
---

```js
function crossProduct(a, b) {
if (a.length !== 3 || b.length !== 3) {
throw new Error('Vectors must be 3-dimensional');
}

return [
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0]
];
}

// Usage:
crossProduct([1, 2, 3], [4, 5, 6]); // Returns: [-3, 6, -3]
```
19 changes: 19 additions & 0 deletions snippets/javascript/mathematical-functions/dot-product.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Dot Product
description: Computes the dot product of two vectors, which is the sum of the products of corresponding elements.
author: JanluOfficial
tags: math,vector-algebra
---

```js
function dotProduct(a, b) {
if (a.length !== b.length) {
throw new Error('Vectors must be of the same length');
}

return a.reduce((sum, value, index) => sum + value * b[index], 0);
}

// Usage:
dotProduct([1, 2, 3], [4, 5, 6]); // Returns: 32
```
21 changes: 21 additions & 0 deletions snippets/javascript/mathematical-functions/error-function.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: Error function
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.
author: JanluOfficial
tags: math
---

```js
function erf(x) {
const sign = Math.sign(x);
const absX = Math.abs(x);
const t = 1 / (1 + 0.3275911 * absX);
const a1 = 0.254829592, a2 = -0.284496736, a3 = 1.421413741, a4 = -1.453152027, a5 = 1.061405429;
const poly = t * (a1 + t * (a2 + t * (a3 + t * (a4 + t * a5))));
return sign * (1 - poly * Math.exp(-absX * absX));
}

// Usage:
erf(-1); // Returns: -0.8427006897475899
erf(1); // Returns: 0.8427006897475899
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Greatest Common Divisor
description: Calculates the largest positive integer that divides each of the integers without leaving a remainder. Useful for calculating aspect ratios.
author: JanluOfficial
tags: math,division
tags: math,division,number-theory,algebra
---

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Least common multiple
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.
author: JanluOfficial
tags: math,number-theory,algebra
---

```js
function lcm(a, b) {
function gcd(x, y) {
while (y !== 0) {
const temp = y;
y = x % y;
x = temp;
}
return Math.abs(x);
}
return Math.abs(a * b) / gcd(a, b);
}

// Usage:
lcm(12,16); // Returns: 48
lcm(8,20); // Returns: 40
lcm(16,17); // Returns: 272
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: Matrix Multiplication
description: Multiplies two matrices, where the number of columns in the first matrix equals the number of rows in the second.
author: JanluOfficial
tags: math,matrix-algebra
---

```js
function matrixMultiply(A, B) {
const rowsA = A.length;
const colsA = A[0].length;
const rowsB = B.length;
const colsB = B[0].length;

if (colsA !== rowsB) {
throw new Error('Number of columns of A must equal the number of rows of B');
}

let result = Array.from({ length: rowsA }, () => Array(colsB).fill(0));

for (let i = 0; i < rowsA; i++) {
for (let j = 0; j < colsB; j++) {
for (let k = 0; k < colsA; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}

return result;
}

// Usage:
matrixMultiply([[1, 2], [3, 4]], [[5, 6], [7, 8]]); // Returns: [[19, 22], [43, 50]]
```
33 changes: 33 additions & 0 deletions snippets/javascript/mathematical-functions/modular-inverse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: Modular Inverse
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.
author: JanluOfficial
tags: math,number-theory,algebra
---

```js
function modInverse(a, m) {
function extendedGCD(a, b) {
if (b === 0) {
return { gcd: a, x: 1, y: 0 };
}
const { gcd, x: x1, y: y1 } = extendedGCD(b, a % b);
const x = y1;
const y = x1 - Math.floor(a / b) * y1;
return { gcd, x, y };
}

const { gcd, x } = extendedGCD(a, m);

if (gcd !== 1) {
return null;
}

return (x % m + m) % m;
}

// Usage:
modInverse(3, 26); // Returns: 9
modInverse(10, 17); // Returns: 12
modInverse(6, 9); // Returns: null
```
24 changes: 24 additions & 0 deletions snippets/javascript/mathematical-functions/prime-number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: Prime Number
description: Checks if a number is a prime number or not.
author: JanluOfficial
tags: math,number-theory,algebra
---

```js
function isPrime(num) {
if (num <= 1) return false; // 0 and 1 are not prime numbers
if (num <= 3) return true; // 2 and 3 are prime numbers
if (num % 2 === 0 || num % 3 === 0) return false; // Exclude multiples of 2 and 3

// Check divisors from 5 to √num, skipping multiples of 2 and 3
for (let i = 5; i * i <= num; i += 6) {
if (num % i === 0 || num % (i + 2) === 0) return false;
}
return true;
}

// Usage:
isPrime(69); // Returns: false
isPrime(17); // Returns: true
```
Loading