필터 지우기
필터 지우기

how to make a function that take an integer as input and return ture if this prime otherwise false

조회 수: 7 (최근 30일)
Hi everyone; I am going to attempt that question: Write a function myprime that takes n, a positive integer, as an input and returns true if n is prime or returns false otherwise. Do not use the isprime or primes or factor built-­‐in functions. Hint: you can use the rem or fix functions. I am using that code
function prime=myprime(n)
a=abs(n);
i=1;
while i<a
if rem(a,i)~=0
prime='TURE';
else
prime='FALSE';
end
i=i+1;
end
end
and getting error
Feedback: Your function made an error for argument(s) 2
Your solution is _not_ correct.
Guide me where i need corrections in my code. Thanks in advance for assistance..
  댓글 수: 3
Ryan Livingston
Ryan Livingston 2015년 5월 28일
I removed the product tag for "MATLAB Coder" below since this example is not using the product MATLAB Coder. Please only use that product tag when this product is involved. Thank you.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2015년 5월 28일
function p = myprime(n)
p = n-nnz(rem(n,1:n)) == 2;
  댓글 수: 2
Muhammad Usman Saleem
Muhammad Usman Saleem 2015년 5월 28일
@Andrei thanks for contributions... Please mark comments in 2nd line of your code so that i can understand what is actual manipulating here
Andrei Bobrov
Andrei Bobrov 2015년 5월 29일
a = rem(n,1:n) % determining the remainder of division n on a series of numbers 1: n
b = nnz (a); % the number of divisions with remainder
out = n-2 == b; % condition for prime number

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

B.k Sumedha
B.k Sumedha 2015년 5월 28일
function result = isprime2(number)
number=5;
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
Returns 1 if the number isprime and returns 0 if the number is not prime.
  댓글 수: 1
Muhammad Usman Saleem
Muhammad Usman Saleem 2015년 5월 28일
@B.k Sumedha thanks for contribution i am now using that code
function result = myprime(n)
n=5;
result=true;
if floor(n)~=n || n<0
result=false;
return
end
for k=2:(n/2)
if rem(n,k)==0
result=false;
return
end
end
but when i test this code i got an error
Feedback: Your function performed correctly for argument(s) 2
Feedback: Your function performed correctly for argument(s) 3
Feedback: Your function made an error for argument(s) 4
Your solution is _not_ correct.

댓글을 달려면 로그인하십시오.


charu sharma
charu sharma 2015년 8월 20일
For number n being prime, if it is divisible by any of the numbers from 2 to n/2 then it is not prime so you just need to check the divisibility till n/2. MATLAB uses 0 or 1 in place of false and true. You can refer this too for less time complexity: http://farzicoders.blogspot.in/2015/08/write-function-myprime-that-takes-n.html

카테고리

Help CenterFile Exchange에서 Discrete Math에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by