forked from dostonnabotov/quicksnip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dostonnabotov#179 from JanluOfficial/main
[Snippets] Added several new mathematical snippets for JavaScript.
- Loading branch information
Showing
10 changed files
with
305 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
snippets/javascript/mathematical-functions/combinations.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
snippets/javascript/mathematical-functions/cross-product.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
snippets/javascript/mathematical-functions/error-function.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
snippets/javascript/mathematical-functions/least-common-multiple.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
34 changes: 34 additions & 0 deletions
34
snippets/javascript/mathematical-functions/matrix-multiplication.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
snippets/javascript/mathematical-functions/modular-inverse.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
snippets/javascript/mathematical-functions/prime-number.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |