필터 지우기
필터 지우기

Writing a function to find prime numbers

조회 수: 71 (최근 30일)
Daniel Kingsley
Daniel Kingsley 2015년 8월 19일
댓글: Steven Lord 2021년 4월 27일
This is homework. I already turned it in. I just don't know why this function doesn't work. I just want to learn what I missed. Thanks so much. This is the problem:
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.
Here is my code:
function result = myprime(n)
%%initially set output flag to true
result = true;
%%iterate over all positive integers 2,3,...,n-1
%%if n is not divisible by any of these factors....it is prime
if (n == 1)
result = 'false';
elseif (n == 2)
result = 'true';
else
for i=2:n-1,
if (mod(n,i)==0)
result = 'false';
end
end
end
%%return "true" or "false" instead of 1 or 0
if (result)
result = 'true';
else
result = 'false';
end
  댓글 수: 1
David Young
David Young 2015년 8월 19일
Please format your code - it's not readable as you can see. (There's a "{} Code" button.)

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

채택된 답변

James Tursa
James Tursa 2015년 8월 19일
편집: James Tursa 2015년 8월 19일
The basic problem is that you are mixing character variables (with the single quotes ' ') with logical variables (without single quotes). E.g.,
result = true; <-- LOGICAL
%%iterate over all positive integers 2,3,...,n-1
%%if n is not divisible by any of these factors....it is prime
if (n == 1)
result = 'false'; <-- CHARACTER
elseif (n == 2)
result = 'true'; <-- CHARACTER
else
for i=2:n-1,
if (mod(n,i)==0)
result = 'false'; <-- CHARACTER
end
end
end
%%return "true" or "false" instead of 1 or 0
if (result) <-- Intent of test is that variable is LOGICAL
result = 'true';
else
result = 'false';
end
So stick to one or the other. E.g., to use logical variables in the first part of your code:
if (n == 1)
result = false;
elseif (n == 2)
result = true;
else
for i=2:n-1,
if (mod(n,i)==0)
result = false;
end
end
end
  댓글 수: 1
Daniel Kingsley
Daniel Kingsley 2015년 8월 19일
@James, Thank you. I am still getting an error for the number 3 as an input. Maybe I should use rem instead of mod?

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

추가 답변 (3개)

Pranash Azrot
Pranash Azrot 2015년 8월 19일
%%Yaar meh edah kitaah see. meerah answer teek see. menu 100% meliah see. try karrey.
function x = myprime(n)
if (rem(n,2)~=0 || n==2)
k = fix(n/2);
else
x = false;
return;
end
for i = 1:k
w(i) = rem(n,i);
end
t=w(w==0);
[m n] = size(t);
if n<=1
x = true;
else
x = false;
return;
end
  댓글 수: 1
Daniel Kingsley
Daniel Kingsley 2015년 8월 20일
@Pranash, I don't understand the comments. I thank you for your help.

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


Irfan Turk
Irfan Turk 2019년 7월 21일
편집: Irfan Turk 2019년 7월 21일
You can find all prime numbers upto a certain number with the following code
%This code find all prime numbers
%upto the entered number
clear all;
N=input('Prime Numbers until:');
if N<2
return;
elseif N==2
disp(2);
return;
end
Pr(1)=2;Pr(2)=3;Count=3;
for i=4:N
C=Check(i);
if C==1
Pr(Count)=i;
Count = Count +1;
end
end
disp(Pr);
function C=Check(i)
C=1;
for k=2:(ceil(sqrt(i)))
if mod(i,k)==0
C=0;
end
end
end
  댓글 수: 3
Walter Roberson
Walter Roberson 2019년 7월 21일
It would make more sense to break after assigning 0 to C.
Irfan Turk
Irfan Turk 2019년 7월 22일
Thank you Walter. That's a good way to make the code better...

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


Farah Salman
Farah Salman 2021년 4월 27일
function y = isPrime(N)
for i=2: floor(N/2)
if mod(N,i)==0
y=false;
return
end
y= true;
end
  댓글 수: 1
Steven Lord
Steven Lord 2021년 4월 27일
This code has at least one bug.
z = isPrime(2)
Output argument "y" (and maybe others) not assigned during call to "solution>isPrime".
function y = isPrime(N)
for i=2:floor(N/2)
if mod(N,i)==0
y=false;
return
end
y= true;
end
end

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by