Skip to content

Instantly share code, notes, and snippets.

@tai2
Created April 18, 2025 08:47
Show Gist options
  • Save tai2/ce426db0adee85a2b2848e56a56472d5 to your computer and use it in GitHub Desktop.
Save tai2/ce426db0adee85a2b2848e56a56472d5 to your computer and use it in GitHub Desktop.
const bessel = (x, alpha, n) => {
if (n < 0) {
throw new Error("n must be non-negative");
}
if (alpha < 0) {
throw new Error("alpha must be non-negative");
}
let sum = 0;
for (let m = 0; m <= n; m++) {
const term =
(Math.pow(-1, m) * Math.pow(x / 2, alpha + 2 * m)) /
(factorial(m) * factorial(alpha + m));
sum += term;
}
return sum;
};
const factorial = (n) => {
if (n < 0) {
throw new Error("n must be non-negative");
}
if (n === 0 || n === 1) {
return 1;
}
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
};
console.log(bessel(1, 0, 5));
console.log(bessel(1, 1, 5));
console.log(bessel(2, 0, 5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment