Question

In: Computer Science

Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater...

Problem 3 Write code in R or Rstudio (Programming)

A prime number is an integer greater than one whose only factors are one and itself. For example, the first ten prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. A twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes. For example, the five twin prime pairs are (3, 5), (5, 7), (11, 13), (17, 19) and (29, 31). (a) Write a function that returns all prime numbers between 1 and a given number n Answer: (b) How many prime numbers are there in the range [1, 100], [1, 1000] and [1, 10000] (c) How many twin prime pairs are there in the range [1, 100], [1, 1000] and [1, 10000] (d) In number theory, the prime number theorem (PNT) indicates that for a large enough n, the number of primes in the range [1, n] approximates to n/log(n). Please verify it by trying wider range.

Solutions

Expert Solution

(a) Write a function that returns all prime numbers between 1 and a given number n Answer :

prime<-function(n)
{
cat("Prime numbers are between 1 and ",n ,"\n")
for(i in 1:n)
{
   c=0
   for(j in 1:i)
       {
   if(i%%j==0)
       {
       c=c+1;
       }
   }
   if(c==2)
{
   cat(i)
cat("\n")
}
}
}
prime(100) #change the value here

Output:

(b) How many prime numbers are there in the range [1, 100], [1, 1000] and [1, 10000]

prime<-function(n)
{
count=0
for(i in 1:n)
{
   c=0
   for(j in 1:i)
       {
   if(i%%j==0)
       {
       c=c+1;
       }
   }
   if(c==2)
{
count=count+1;
     
}
}
cat("\nThe total Prime numbers are ",count," between 1 and ",n)
}
prime(100) #change the value
prime(1000)

out put :

(c) How many twin prime pairs are there in the range [1, 100], [1, 1000] and [1, 10000]

twinprime <- function(n1,n2)
{
count=0;
cat("The twin Prime numbers are:\n")
for(n1 in 1:n2)
{
x<-n1+2
if(Checkprime(n1) && Checkprime(x))
   {
count=count+1
cat(n1," ",x)
cat("\n")
}
}
cat("The total Twin Prime numbers are ",count," between 1 and ",n2)
}
Checkprime <- function(n)
{
f=0
for(i in 1:n)
{
if(n%%i==0)
f<-f+1
}
if(f==2)
TRUE
else
FALSE
  
}

twinprime(1,100) #change the value here

output :

d)In number theory, the prime number theorem (PNT) indicates that for a large enough n, the number of primes in the range [1, n] approximates to n/log(n).

In number theory, the Prime Number Theorem (PNT) describes the asymptotic distribution of the prime numbers among the positive integers.

the first such distribution found is π(n) ~ n / log(n), where π(n) is the prime-counting function and log(n) is the natural logarithm of n.


Related Solutions

RStudio R-Programming Statistics Problem - power.t.test and p-values *I believe we're expected to write code using...
RStudio R-Programming Statistics Problem - power.t.test and p-values *I believe we're expected to write code using for loops? Any guidance will help! 4. For this problem, you will run a simulation to investigate how violating the assumption of normally distributed data can affect the properties of a t-test. a. The gamma distribution is skewed to the right. It contains a parameter called “shape”. The R function for generating data from a gamma distribution is rgamma – you can read the...
A prime number is an integer greater than 1 that is evenlydivisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6.Write a Boolean function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, and false otherwise. Demonstrate...
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. Java
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. b) Modify the...
Write following program using Python: A positive integer greater than 1 is said to be prime...
Write following program using Python: A positive integer greater than 1 is said to be prime if it has no divisors other than 1 and itself. A positive integer greater than 1 is composite if it is not prime. Write a program that asks the user to enter a integer greater than 1, then displays all of the prime numbers that are less than or equal to the number entered. Last, create two files. One file will hold all of...
A prime number (or prime) is a natural number greater than 1 that has no posítive...
A prime number (or prime) is a natural number greater than 1 that has no posítive divisors other than 1 and itself. Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
Write the code to return the output in Rstudio. What is the code? Code: x <-...
Write the code to return the output in Rstudio. What is the code? Code: x <- c(28, 69, 5, 88, 19, 20) Output must be: [1] 4 2 1 6 5 3
a) Write C code using a struct to hold student information: integer id integer number of...
a) Write C code using a struct to hold student information: integer id integer number of hours taken integer number of hours passed double gpa b) create a variable of the type in #25 and initialize it with some data.
This question involves coding in RStudio Problem 3 (Verzani problem 2.22): Write a function to compute...
This question involves coding in RStudio Problem 3 (Verzani problem 2.22): Write a function to compute the average distance from the mean for some data vector.
Prove by strong mathematical induction that any integer greater than 1 is divisible by a prime...
Prove by strong mathematical induction that any integer greater than 1 is divisible by a prime number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT