Let y <- sample(1000, 30, replace = TRUE).
Write functions in R to do the following. Test each function. Determine how many elements of y are prime.
y <- sample(1000, 30, replace = TRUE)
y
# For prime number
is.prime <- function(x)
vapply(x, function(y) sum(y / 1:y == y %/% 1:y), integer(1L)) ==
2L
## Run
(y)[is.prime(y)]
## Number of prime number
n=length((y)[is.prime(y)])
n
####################
#########################
> y <- sample(1000, 30, replace = TRUE)
> y
[1] 56 73 768 262 736 757 542 857 609 360 301 708 500 420 893 981
753 240 823
[20] 819 38 576 320 483 404 817 900 104 746 519
>
> # For prime number
> is.prime <- function(x)
+ vapply(x, function(y) sum(y / 1:y == y %/% 1:y), integer(1L)) ==
2L
>
> ## Run
> (y)[is.prime(y)]
[1] 73 757 857 823
>
> ## Number of prime number
>
> n=length((y)[is.prime(y)])
> n
[1] 4
Get Answers For Free
Most questions answered within 1 hours.