|
400 | 400 | {
|
401 | 401 | "name": "Mathematical Functions",
|
402 | 402 | "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 | + }, |
403 | 447 | {
|
404 | 448 | "title": "Greatest Common Divisor",
|
405 | 449 | "description": "Calculates the largest positive integer that divides each of the integers without leaving a remainder. Useful for calculating aspect ratios.",
|
406 | 450 | "author": "JanluOfficial",
|
407 | 451 | "tags": [
|
408 | 452 | "math",
|
409 |
| - "division" |
| 453 | + "division", |
| 454 | + "number-theory", |
| 455 | + "algebra" |
410 | 456 | ],
|
411 | 457 | "contributors": [],
|
412 | 458 | "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" |
413 | 506 | }
|
414 | 507 | ]
|
415 | 508 | },
|
|
0 commit comments