A prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself. The purpose of this problem is to write a function (say its name is check.prime) to check whether or not a given natural number is a prime. Unless you want to use some other more advanced method, you can write your function based on the so-called “trial division” method. The idea is as follows. For a positive integer n, one only needs to test whether n is a multiple of each integer m between 2 and √n. If n is a multiple of any of these integers then it is a composite number and not a prime; if it is not a multiple of any of these integers then it is a prime. Of course, you can use other methods. For example, you can check how many factors n has. If n has exactly 2 factors (must be positive integers less than or equal to n), then n is a prime.

A prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself. The purpose of this problem is to write a function (say its name is check.prime) to check whether or not a given natural number is a prime. Unless you want to use some other more advanced method, you can write your function based on the so-called “trial division” method. The idea is as follows. For a positive integer n, one only needs to test whether n is a multiple of each integer m between 2 and √n. If n is a multiple of any of these integers then it is a composite number and not a prime; if it is not a multiple of any of these integers then it is a prime. Of course, you can use other methods. For example, you can check how many factors n has. If n has exactly 2 factors (must be positive integers less than or equal to n), then n is a prime.

The input of check.prime should be a positive integer and the output should be a list containing a logic variable called result and a character variable called report, which tells if the input is a prime. Specific requirements are implied from the following examples.

  1. If one feeds anything but a positive integer, assign NA to result and report will be “Error! The input must be a positive integer”.
  2. We know 37 is a prime number. Implementing check.prime(37) will yield a T or TRUE for result and “The input 37 IS a prime number.” for report.
  3. Implementing check.prime(51) will yield a F or FALSE for result and “The input 51 IS NOT a prime number.” for report.

Complete the following tasks and present your output.

(i). Implement your R function with 5, 21, 47.5, 63, 87, and 111 one by one and show your results.

(ii). Implement x=floor(100*runif(1)); check.prime(x) for three times and display your output.

Need help with your question? Submit here

Implement your R function with 5, 21, 47.5, 63, 87, and 111 one by one and show your results.

R program code available for just  $3.00. Kindly Chat us on Whatsapp

Related Questions and Answers

Predicting health care charges of insurance beneficiaries (30 points). Health insurance companies often have to cover a sizable portion of the health care costs of their primary beneficiaries. Thus, it is of practical interest for these companies to predict the cost of medical bills from individual patient characteristics. In this problem, we will consider predicting the medical costs (or charges) in dollars for 𝑛 = 1338 primary beneficiaries

Check Solution »

Predicting birthweight based on maternal risk factors (34 points). In this problem, we will consider predicting the birthweight (birthwt) of newborns in milligrams, using the age of the mother at the time of delivery (age), the weight of the mother at the last menstrual period (lwt) in kilograms, and the mother’s race (race)

Check Solution »

Predicting home sales prices from square footage (36 points). In this problem, we will consider predicting house sale prices (SalePrice) from square footage (SqrFeet) using a dataset of 𝑛 = 506 houses.

Check Solution »

Share this question:

Facebook
Twitter
Pinterest
LinkedIn
WhatsApp

Get Step-by-Step Solutions

Experience expert help with your homework
RECENT REVIEWS
Kimberly
Kimberly
Statistics
Read More
Excellent work. Meet my expectations. Thanks.
John
John
Math
Read More
" Learnmathstat.com " is a name that MUST remember when you have a project in mathematics, even if that project is related to an advanced course!
Eva
Eva
Algebra
Read More
Very professional, high quality, and always delivers on time.
Previous
Next
Scroll to Top