On Rosetta Code, the list of Tasks uncompleted in R contains the following challenge: print the Bernoulli numbers from B0 to B60. After seeing some solutions, including one in in Python , I wanted to try in R. There are examples of code on that website, and a nice description of the algorithm here.
library(MASS)
library(pander)
n <- 20L
js <- 0:n
Bs <- lapply((n+1):1,function(x) numeric(x))
Bs[[1]] <- 1/(js+1)
for(i in 2:length(Bs)){
Bs[[i]] <- seq_along(Bs[[i]])*(Bs[[i-1]][-length(Bs[[i-1]])] - Bs[[i-1]][-1])
}
Bn <- sapply(Bs,"[",1)
pander(cbind(js,zapsmall(Bn)))
Bn | value |
---|---|
0 | 1.000 |
1 | 0.500 |
2 | 0.167 |
3 | 0.000 |
4 | -0.033 |
5 | 0.000 |
6 | 0.024 |
7 | 0.000 |
8 | -0.033 |
9 | 0.000 |
10 | 0.076 |
11 | 0.000 |
12 | -0.253 |
13 | 0.000 |
14 | 1.167 |
15 | 0.001 |
16 | -7.069 |
17 | 0.641 |
18 | 69.997 |
19 | 323.014 |
20 | 5986.642 |
The value of every odd bernoulli number > 3 should be 0. But, I start getting nonzero values after 15