用matlab编写一个可以判断质数的调用函数

目的:用matlab编写一个可以判断质数的函数。

          1)若是质数,返回1;

          2)若不是,返回0。

代码:

function s = prime_22(n)
for i = 2 : n-1
    if mod(n,i)==0
        s = 0;   % s = 0 表示为合数
        break; 
    else

        s = 1;   % s = 1 表示为质数
    end
end   
end

结果:

猜你喜欢

转载自blog.csdn.net/weixin_41983807/article/details/82884805
今日推荐