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

Write a MATLAB program to determine whether or not a number is a prime number. A

ID: 3560088 • Letter: W

Question

Write a MATLAB program to determine whether or not a number is a prime number. A prime number is a number only divisible by 1 and itself. Therefore you want to write code to make sure the remainder of division by any number between 3 and the number is no t zero. We know 1 and 2 are prime numbers. We also know that any even number larger than 2 is not prime. Therefore you only have to test the odd values between 3 and the square root of the number. This is true because if there is a factor larger then i t is multiplied by a number less than the square root. Use a while loop to do the division as long as the remainder is not zero and the loop value is less than the square root of the input value. Once the while loop is finished print a message telling th e user whether or not the number entered is a prime number. Test it with both a prime and a non - prime number

Explanation / Answer

disp('input a pair of natural numbers, N and M with M>N');
N=input('N='); M=input('M=');

for j=0:(M-N)
   Prime=1;
   i = 2;
   while(Prime!=0)

       if mod((N+j),i)==0
       Prime=0;
       i = i+1;
       end
      
       if i> (N+j)/2
       break;
       end
   end

   if Prime==1
       disp('This is a Prime Number');
   else disp('Not Prime');

   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