Created
May 22, 2019 12:26
-
-
Save qti3e/82c3e47cac225b2fa65810c4b5904ace to your computer and use it in GitHub Desktop.
Generate first n Bernoulli numbers
This file contains hidden or 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
function b(max) { | |
const B = Array(max + 1).fill(0); | |
B[0] = 1; | |
B[1] = 1/2; | |
for (let n = 2; n <= max; n += 2) { | |
let sum = 0; | |
let c = 1; | |
for (let j = 0, j2 = n; j <= n / 2; j += 2, j2 -= 2) { | |
let tmp = (j - 1) / (n + 1 - j) * B[j]; | |
if (j2 !== j) tmp += (j2 - 1) / (n + 1 - j2) * B[j2]; | |
tmp *= c / n; | |
sum += tmp; | |
c *= (n - j) * (n - j - 1) / ((j + 1) * (j + 2)); | |
} | |
sum *= n % 2 ? 1 : -1; | |
B[n] = sum; | |
} | |
return B; | |
} | |
console.time("B(20000)"); | |
b(20000); | |
console.timeEnd("B(20000)"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment