Question

Let y <- sample(1000, 30, replace = TRUE). Write functions in R to do the following....

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How to do the following in R: Write a function to generate a random sample of...
How to do the following in R: Write a function to generate a random sample of size n from the Beta(a,b) distribution by the acceptance-rejection method. Generate a random sample of size 1000 from the Beta(3,2) distribution.
How to do the following in R: Write a function to generate a random sample of...
How to do the following in R: Write a function to generate a random sample of size n from the Gamma(α,1) distribution by the acceptance-rejection method. Generate a random sample of size 1000 from the Gamma(3,1) distribution. (Hint: you may use g(x) ∼ Exp(λ = 1/α) as your proposal distribution, where λ is the rate parameter. Figure out the appropriate constant c).
How to do the following in R: Write a function to generate a random sample of...
How to do the following in R: Write a function to generate a random sample of size n from the Gamma(α,1) distribution by the acceptance-rejection method. Generate a random sample of size 1000 from the Gamma(3,1) distribution. (Hint: you may use g(x) ∼ Exp(λ = 1/α) as your proposal distribution, where λ is the rate parameter. Figure out the appropriate constant c).
(a) Let the statement, ∀x∈R,∃y∈R G(x,y), be true for predicate G(x,y). For each of the following...
(a) Let the statement, ∀x∈R,∃y∈R G(x,y), be true for predicate G(x,y). For each of the following statements, decide if the statement is certainly true, certainly false,or possibly true, and justify your solution. 1 (i) G(3,4) (ii) ∀x∈RG(x,3) (iii) ∃y G(3,y) (iv) ∀y¬G(3,y)(v)∃x G(x,4)
How to code this in python Write a program that computes and prints the first 1000...
How to code this in python Write a program that computes and prints the first 1000 prime numbers - simply write out each prime number on a new line. In implementing this solution I want you to define 2 functions: is_prime which can be used to test whether a number is a prime (e.g. is_prime(17) returns True but is_prime(9) returns False) and compute_primes which will return an array (or Python list) of the first n primes where n is passed...
Write a sketch in Arduino with two functions; the first function-1 find the value “y” in...
Write a sketch in Arduino with two functions; the first function-1 find the value “y” in the equation: ? = ? − 30 . The value “x” is entered by keyboard. The second function-2 test whether the value “y” (calculated with the first function) is a negative number. If it is true, then the function call again function-1 with the value a new value x = x (old value) + 40. Otherwise print the message: “Finished”. Print all results.
Let p and q be any two distinct prime numbers and define the relation a R...
Let p and q be any two distinct prime numbers and define the relation a R b on integers a,b by: a R b iff b-a is divisible by both p and q. I need to prove that: a) R is an equivalence relation. (which I have) b) The equivalence classes of R correspond to the elements of  ℤpq. That is: [a] = [b] as equivalence classes of R if and only if [a] = [b] as elements of ℤpq I...
Write the following functions and provide a program to test them (main and all functions in...
Write the following functions and provide a program to test them (main and all functions in one .py). 6 pts def allTheSame(x, y, z) (returning true if the arguments are all the same) def allDifferent(x, y, z) (returning true if the arguments are all different) def sorted(x, y, z) (returning true if the arguments are sorted, smallest one first keep it simple, python
Program has to be written in ML language. Write functions in ML to do the following:...
Program has to be written in ML language. Write functions in ML to do the following: 1. Given a list, return that list with its first and third elements deleted. Assume the length of the list is at least 3. 2. Given a list of real pairs, return the list containing the larger element in each pair. Examples: larger_in_pair([]) = [] larger_in_pair([(1.0,2.5),(4.7,3.6),(5.5,8.8)] = [2.5,4.7,8.8] 3. Given two lists of integers, return the list of the sums of the elements in...
Write the following in C: 2. An integer is said to be prime if it is...
Write the following in C: 2. An integer is said to be prime if it is divisible by only 1 and itself. Write a function called prime() that takes in one integer number, and returns 1 (true) if the number is prime and 0 (false) otherwise. Write a program to generate six random numbers between 1 to 100 and calls function prime() on each one to determine if it is prime or not.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT