Published on 28 April 2022 (Updated: 25 January 2023)

Welcome to the Prime Number in Matlab page! Here, you’ll find the source code for this program as well as a description of how the program works.

Current Solution

functionresult=prime_number(n)ifn<0result='Please input a non-negative integer'endifnot(ischar(n))result='Please input a non-negative integer'endifnot(isempty(n))=1result='Please input a non-negative integer'endifnot(~isinf(x)&floor(x)==x)result='Please input a non-negative integer'endifn==0|n==1result='Neither Prime nor Composite'endm=2;isprime=1;while(m<=sqrt(n))if(rem(n,m)==0)isprime=0;break;endm=m+1;endif(isprime==1)result='Prime'elseresult='Composite'end