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

In MATLAB, the isprime () function is used to check whether or not a given numbe

ID: 3932929 • Letter: I

Question

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 ------------

function flag=isprime(x)

flag=0

i=2

while(i<=x/2)

if(x%i==0)

flag=1

end

i++

end

end

Script 1

count=0

i=0

primevect=[]

while(count<20&&i<=34)

if(isprime(i)==0)

primevect[count]=i

count++

end

i++

end

Script 2

count=0

primevect=[]

while(count<20)


a = 2; b = 101;

i= (b-a).*rand(20,1) + a;

if(isprime(i)==0)

primevect[count]=i

count++

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