SpinningAtInfinity - 1 year ago 118

R Question

I have a code

`Eratosthenes <- function(n) {`

if (n >= 2) {

sieve <- seq(2,n)

primes <- c()

for (i in seq(2,n)) {

if (any(sieve == i)) {

primes <- c(primes, i)

sieve <- c(sieve[(sieve %% i) ! = 0], i)

}

}

return(primes)

} else {

stop("Input value of n should be at least 2.")

}

}

with the error

`Error: unexpected '}' in "}"`

`}`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

The problem appears to be here: `sieve <- c(sieve[(sieve %% i) ! = 0], i)`

. I'm assuming that you want "is not equal to", so should be `sieve <- c(sieve[(sieve %% i) != 0], i)`

.

Here's the formatted code that works for me:

```
Eratosthenes <- function(n) {
if (n >= 2) {
sieve <- seq(2,n)
primes <- c()
for (i in seq(2,n)) {
if (any(sieve == i)) {
primes <- c(primes, i)
sieve <- c(sieve[(sieve %% i) != 0], i)
}
}
return(primes)
} else {
stop("Input value of n should be at least 2.")
}
}
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**