1

I’ve faced a problem in r. My task is to choose the smallest natural number (from N) which cube root also is natural (what means it belongs to N). I have code like this:

N <- c(4:500)
N2 <- numeric(1)

for(i in 1:length(N)) {
    if(N[i]^(1/3) %in% N){
       N2 = N[i] 
       break
    }
}
N2

If N<-c(2:500) value for N2 should be 8 and it works correctly. In other case, if N<-c(3:500) N2 vector is equal to 27 and it is also a good value. The problem appears in other cases, for instance N<-c(4:500). It looks like %in% operator doesn’t work correctly. Would you have an idea why?

kabanus
  • 24,623
  • 6
  • 41
  • 74
Anna
  • 11
  • 1

0 Answers0