Question

In: Computer Science

A twin prime is a pair of primes (x, y), such that y = x +...

A twin prime is a pair of primes (x, y), such that y = x + 2. Construct a list of all twin primes less than 1000. The result should be stored in a list of numeric vectors called twin_primes, whose elements are the twin primes pairs. Print the length of the list twin_primes and print the 10th and the 15th elements of the list, i.e. twin_primes[[10]] and twin_primes[[15]].

Please use R-code.

Solutions

Expert Solution

SOLUTION -

CODE -

TwinPrimesLong = function(n) {

if (n < 5) stop("Input value of n should be at least 5.")

primesLessThanN = Eratosthenes(n)

#Combine differenced sets with original prime set
twin_Prime = cbind( (primesLessThanN-2) ,primesLessThanN)

#Keep only those rows where differenced numbers are also primes i.e. belong to prime set
twin_Prime = data.frame(matrix(twin_Prime[(primesLessThanN-2) %in% primesLessThanN,],ncol=2))

colnames(twin_Prime)=c("TwinPrime1","TwinPrime2")

cat("The twin primes less than n = ",n,"in row form are :\n")

cat(paste0("(",twin_Prime[,1],",",twin_Prime[,2],")",collapse=","),"\n")

cat("\n\nThe twin primes less than n = ",n,"in table form are :\n")

print(twin_Prime)


}

TwinPrimesShort = function(n) {

if (n < 5) stop("Input value of n should be at least 5.")

primesLessThanN = Eratosthenes(n)


#Use diff function to compute difference with previous value and check if == 2
TwinPrime1 = primesLessThanN[diff(primesLessThanN)==2]
TwinPrime2 = TwinPrime1 + 2

#Keep only those rows where differenced numbers are also primes i.e. belong to prime set
twin_Prime = data.frame(TwinPrime1=TwinPrime1,TwinPrime2=TwinPrime2)

cat("The twin primes less than n = ",n,"in row form are :\n")

cat(paste0("(",twin_Prime[,1],",",twin_Prime[,2],")",collapse=","),"\n")

cat("\n\nThe twin primes less than n = ",n,"in table form are :\n")

print(twin_Prime)

}


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

Leigh chooses the primes 17 and 13, and an encryption key which is relatively prime to...
Leigh chooses the primes 17 and 13, and an encryption key which is relatively prime to φ(17*13) = 192. He chooses 7. He posts his parameters (7, 221). Then he receives the encrypted message 72 from Bai. What is the plaintext version of this message
Searching for Primes Recall that a prime number is divisible only by itself and one. Assume...
Searching for Primes Recall that a prime number is divisible only by itself and one. Assume that we are given a list of all the prime numbers from 1 to 10,000, in sorted order in a text file called primes.txt: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,...
Find the solution of the system x′=−4y, y′=3x, where primes indicate derivatives with respect to t,...
Find the solution of the system x′=−4y, y′=3x, where primes indicate derivatives with respect to t, that satisfies the initial condition x(0)=1, y(0)=−1.
Data can be collected and organized as an ordered pair (x, y). The data can be...
Data can be collected and organized as an ordered pair (x, y). The data can be analyzed to determine the type and strength of a correlation and to calculate a regression line in order to make a prediction. Use the internet to find a data set of ordered pairs. Key terms to search: Free Public Data Sets and Medical Data Sets. Create a Post: Introduce your Data Set. Which would be the independent variable, and which would be the dependent...
If p,p+2 are twin primes, prove 4((p−1)!+1)+p≡0 modp(p+2)
If p,p+2 are twin primes, prove 4((p−1)!+1)+p≡0 modp(p+2)
Consider rolling two dice and let (X, Y) be the random variable pair defined such that...
Consider rolling two dice and let (X, Y) be the random variable pair defined such that X is the sum of the rolls and Y is the maximum of the rolls. Find the following: (1) E[X/Y] (2) P(X > Y ) (3) P(X = 7) (4) P(Y ≤ 4) (5) P(X = 7, Y = 4)
Consider a statistical expirement of flipping a pair of fair coins simultaneously. Let X & Y...
Consider a statistical expirement of flipping a pair of fair coins simultaneously. Let X & Y be the number heads in flipping each coin. Define a joint density function Z = XY. (I provided the answers in bold but I need help understanding how it is solved. Thank you in advance). a) The number of possible distinct values of Z is: 2 b) The probability of Z = 0 is: 3/4 c) The mean of Z is: 1/4 d) The...
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in...
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in the range from start_num to end_num (inclusive). For example, start_num = 5 and end_num = 12, the prime numbers in that range are: [5, 7, 11]. The smallest gap between any two prime numbers in this list is 2, so the function would return 2. You may want to modify your solution from Problem 1 on Assignment 4, or you can use this starter...
Product X is manufactured by Y Co. Direct materials cost £6.10 and prime costs total £9.60...
Product X is manufactured by Y Co. Direct materials cost £6.10 and prime costs total £9.60 per unit of product. Production overheads are absorbed at a rate of £13.40 per machine hour. Two units of Product X are manufactured per machine hour. Using absorption costing, what is the total production cost per unit of Product X? £22.40 £16.30 £23.00 £29.10
a) y''(x)-3y'(x)=8e3x+4sinx b) y''(x)+y'(x)+y(x)=0 c) y(iv)(x)+2y''(x)+y(x)=0
a) y''(x)-3y'(x)=8e3x+4sinx b) y''(x)+y'(x)+y(x)=0 c) y(iv)(x)+2y''(x)+y(x)=0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT