Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a MATLAB function called twinPrime that takes two positive integers n1 and

ID: 3775929 • Letter: W

Question

Write a MATLAB function called twinPrime that takes two positive integers n1 and n2 as inputs. The function must

a) return a 2-column array containing the twin primes found within the range given by the function inputs (both twin values should fall within the range).

b) return the value of how many twin prime pairs were contained in the requested range

c) Include help comments and other descriptive comments throughout your code

d) not accept nonsensical input

e) display the statement x1. x2 and x3 are twin primes in your range for each pair of twin primes, each on a new line.

Explanation / Answer

%the following function takes two inputs and finds all twin within that range
function [twinprimes,count]=tprimes(n1,n2)
twinprimes = [,]; %array to store twin primes
count=0;
for i = n1: 2: n2 %primes greater than 2 are always odd, so no point testing the evens
if isprime(i) && isprime(i+2) %n is the smallest of a pair of twin primes if n is prime and so is n+2
twinprimes(length(twinprimes)+1) = i; %storing twin primes.
twinprimes(length(twinprimes)+1) = i+2;
count++;
end
end
fprintf('%d %d are twin primes ' , twinprimes) %printing tein primes
fprintf('Total twinprimes in given range: %d ' , count)
end

sample outpur for range 1 to 100

3 5      are twin primes                                                                                                                                                        

5 7      are twin primes                                                                                                                                                        

11 13    are twin primes                                                                                                                                                        

17 19    are twin primes                                                                                                                                                        

29 31    are twin primes                                                                                                                                                        

41 43    are twin primes                                                                                                                                                        

59 61    are twin primes                                                                                                                                                        

71 73    are twin primes                                                                                                                                                        

Total twinprimes in given range: 8

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote