4 ms·
I think b/c it's using the difference table method in both cases. Where as, if the pattern starts with 1, it recognizes it as powers of 2. R code: solver
by graywh 17y ago
I think b/c it's using the difference table method in both cases. Where as, if the pattern starts with 1, it recognizes it as powers of 2.
R code:
solver <- function(x) {
print(x)
if (length(x) == 0 || all(x == 0))
0
else
x[length(x)] + solver(diff(x))
}
solver(c(0,1,2,4,8,16,32,64,128)) #=> 255
solver(c(0,1,2,4,8,16,32,64,128,256)) #=> 512
solver(c(0,1,2,4,8,16,32,64,128,256,512)) #=> 1023
solver(c(0,1,2,4,8,16,32,64,128,26,512,1024)) #=> 2048