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: 3933925 • 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

1)

isprime(A) returns an array the same size as A containing logical true (1) for the elements of A which are prime, and logical false (0) otherwise. A must contain only positive integers.

Examples

c = [2 3 0 6 10]

c =

     2     3     0     6    10

isprime(c)

ans =

     1     1     0     0     0

a) function f = checkforprimeV2 (n)
divider = n;

for ii = 1:length(n)
    counter= 0;
    check = n(ii) / 2;
    check2 = floor(check);
    
    if check ~= check2
        
        while divider(ii) > 1 && counter <= 1 ,
        divider(ii) = (divider(ii) - 1);
        p = n(ii) / divider(ii);
        
        if p == floor(p)
        counter = counter + 1;
        end
        end
        
    else
        counter = 2;
        f(ii) = 0;
    end
    
    if counter > 1
        f(ii) = 0;
   
    else
        f(ii) = 1;
    end
end
end

b)

You have to choose 2 random prime numbers, it's right?

I think you can do it in this way:

p=[4 4];
for i=1:2
while isprime(p(i))==0
p(i)=randint(1,1,1000);
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