Reputation: 41
How can you develop a series/sequence of 100 iterations using a for-loop or a while-loop?
f <- c()
while(length(f) < 100) {
a <- 1
f <- c(f, a)
b <- 0
f <- c(f, b)
}
print(f)
I have the correct output but the input is not correspondent with Grandi's series.
Upvotes: 0
Views: 57
Reputation: 41
Found how:
g <- 1
for(i in seq_along(1:100)) {
g[[i + 1]] <- g[[i]] + (-1)^i
}
g
Upvotes: 2