matlab程序,检查给定的输入数是否为素数

时间:2021-11-10 07:21:31

I need to find whether the given input number in MAT-LAB command window is prime number or not. I need to code without using the inbuilt command in matlab.

我需要找到matlab命令窗口中给定的输入号是否为质数。我需要不使用matlab中的内置命令进行编码。

4 个解决方案

#1


2  

Am I programming your homework?

我给你的家庭作业编程了吗?

function result = isprime2(number)

result=true;

%% check if number is a nonnegative integer
if floor(number)~=number || number<0
    result=false;
    return
end

%% check if number can be divided by another integer
for k=2:(number/2)
    if rem(number,k)==0
        result=false;
        return
    end
end

#2


1  

The above answer is not correct....it consider 9 as prime number which is not

上面的答案是不正确的....它认为9是质数,但它不是

function result=myprime(n)
%result=true;

%check if number is a nonnegative integer
if floor(n)~=n || n<0
result=false;
return
end
% check if number can be divided by another integer
for k=2:(n/2)
if rem(n,k)==0
    result=false;
    return
end
end
result=true;
return 
end

#3


0  

My program goes this way

我的程序是这样的。

function tf = isprim(n)
%this function will check whether the number is prime or not
tf = true;
for i = 2:n-1
    if rem(n,i) == 0
        tf = false;
        break
    end
end

Source: Radius of Circle

来源:圆半径

#4


0  

Calculation

计算

a = input('Enter a value:  ');
b  = input ('Enter b  value:  ');

for(x = a : b)
    if (isprime(x)==1)
        fprintf('%d is prime number \n',x)
    end
end

#1


2  

Am I programming your homework?

我给你的家庭作业编程了吗?

function result = isprime2(number)

result=true;

%% check if number is a nonnegative integer
if floor(number)~=number || number<0
    result=false;
    return
end

%% check if number can be divided by another integer
for k=2:(number/2)
    if rem(number,k)==0
        result=false;
        return
    end
end

#2


1  

The above answer is not correct....it consider 9 as prime number which is not

上面的答案是不正确的....它认为9是质数,但它不是

function result=myprime(n)
%result=true;

%check if number is a nonnegative integer
if floor(n)~=n || n<0
result=false;
return
end
% check if number can be divided by another integer
for k=2:(n/2)
if rem(n,k)==0
    result=false;
    return
end
end
result=true;
return 
end

#3


0  

My program goes this way

我的程序是这样的。

function tf = isprim(n)
%this function will check whether the number is prime or not
tf = true;
for i = 2:n-1
    if rem(n,i) == 0
        tf = false;
        break
    end
end

Source: Radius of Circle

来源:圆半径

#4


0  

Calculation

计算

a = input('Enter a value:  ');
b  = input ('Enter b  value:  ');

for(x = a : b)
    if (isprime(x)==1)
        fprintf('%d is prime number \n',x)
    end
end