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

Prime Numbers In MATLAB, the isprime () function is used to check whether or not

ID: 3929168 • Letter: P

Question

Prime Numbers In MATLAB, the isprime () function is used to check whether or not a given number is prime. By combining this function with a while loop and other MATLAB functions (which you have learned), it is possible to generate matrices of prime numbers (a feat that is not normally possible using analytical methods, since prime numbers have no pattern). Write a script that will create a 20 element column vector of the first twenty prime numbers above 34. Write a script that will create a 20 element column vector of random prime numbers between 2 and 101.

Explanation / Answer

function result = isprime2(number)

result=true;

%% check if number is a nonnegative integer
if floor(number)~=number || number<0
result=false;
return
end

%% check if number can be divided by another integer
for k=2:(number/2)
if rem(number,k)==0
result=false;
return
end
end

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