Nonce
Nonce

Reputation: 41

Grandi's series/sequence using a for-loop in R?

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

Answers (1)

Nonce
Nonce

Reputation: 41

Found how:

g <- 1
for(i in seq_along(1:100)) {
  g[[i + 1]] <- g[[i]] + (-1)^i
}

g

Upvotes: 2

Related Questions