Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206278

Error in Repeat Loop - if/else statement problem

$
0
0

I'm trying to find the highest Fibonacci number before 5000. this is the code I have so far:

n <- 5000
fib <- c()
fib[1] <- 1
fib[2] <- 1
c <- 1
i <- 3
repeat {
  fib[i] <- fib[i-1] + fib[i-2]
  i <- i+1
  c <- c+1
  if (fib[i]>=5000) {
    break
  }
h <- fib[c]
print(h)
}

The error I receive is:

Error in if (fib[i] >= 5000) { : missing value where TRUE/FALSE needed

Any thoughts on how to resolve? I know my code isn't perfect, but I'm learning :)


Viewing all articles
Browse latest Browse all 206278

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>