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

Hand in your MATLAB program and output. Problem 5: Write a function that finds a

ID: 3714600 • Letter: H

Question

Hand in your MATLAB program and output. Problem 5: Write a function that finds all the prime numbers between two numbers m and n. Name the function pr-prime (m, n),where the input arguments m and n are positive integers, and the output argument pr is a vector with the prime numbers. If m > n is entered when the function is called, the error message "The value of n must be larger than the value of m." is displayed. If a negative number or a number that is not an integer is entered when the function is called, the error message "The input argument must be a positive integer." is displayed. Use the function with: (a) prime (12,80) (b) prime (21,635) (c) prime (100,200) (d)prime (90,50) Hand in your MATLAB program and output.

Explanation / Answer


Save the following code in a file named prime.m

function pr = prime(m, n)
if m < 0 || floor(m) ~= m
error('The input argument m should be a positive integer')
end
  
if n < 0 || floor(n) ~= n
error('The input argument n should be a positive integer')
end
  
if n < m
error('The value of n should be larger than value of m')
end
  
pr = [];
for i = m:n
if isprime(i)
pr = [ pr, i];
end
end
end

Now you can call this function in command window using prime(100, 200) or prime(-10, 5) etc... either to return vector of primes in between the given arguments or display an error message for invalid arguments
Please do rate the answer if it helped. Thank you


output
----
> prime(12, 80)
ans =

Columns 1 through 16:

13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73

Column 17:

79

> prime(100, 200)
ans =

Columns 1 through 13:

101 103 107 109 113 127 131 137 139 149 151 157 163

Columns 14 through 21:

167 173 179 181 191 193 197 199

> prime(90, 50)
error: The value of n should be larger than value of m
error: called from
prime at line 11 column 8
> prime(21, 63.5)
error: The input argument n should be a positive integer
error: called from
prime at line 7 column 9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote