I need to write a function which prints out prime number between the two arguments.

I have to write a function which prints out all the prime numbers between the two arguments. My code is working fine, it gives out all the prime numbers but it also gives unnecessary things.
Here is the code:
function [Prime] = PrimeNum (N,M)
if (N>M || N <0 || M < 0)
error('ERROR: Invalid Input, you entered biggere number first or you entered negative number as you interval.');
end
for j=0:(M-N)
Prime=1;
for i=2:((N+j)/2)
if mod((N+j),i)==0
Prime=0;
end
end
if Prime==1
disp(N+j);
end
end
This is the output [p] = PrimeNum(3,15) 3
5
7
11
13
p =
0
I need output to be like this p = 3
5
7
11
13
Some how i need to get rid of p= 0 from the output, how do I do that?

 Accepted Answer

variant for Faras Momin:
function [Prime] = PrimeNum3(N,M)
if (N>M || N <0 || M < 0)
error('ERROR: Invalid Input, you entered biggere number first or you entered negative number as you interval.');
end
Prime = [];
for j=0:(M-N)
if all(mod((N+j),2:((N+j)/2))), Prime = [Prime;N+j]; end
end

1 Comment

Interesting. I would suggest an optimization,
all(mod((N+j),2:((N+j)/2)))
to
all(mod(N+j, [2, 3:2:sqrt(N+j)])

Sign in to comment.

More Answers (5)

Invoke
[p] = PrimeNum(3,15);
with the semi-colon on the end.
Note: the answer would be different if you needed to return the prime numbers, not just print them out.

2 Comments

I tried [p] = PrimeNum(3,15); but my professor told me to make changes in the function.
Can you give more detail/example about the note you wrote, about return.
The line "function [Prime] = PrimeNum (N,M)" means that when the code returns to the caller, whatever is then in the variable "Prime" is to be returned to the caller. If you examine your code, you will find that what is in variable "Prime" at the end of your routine will be 1 if the last number in the range is prime and 0 if the last number in the range is composite. Chances are, though, that your assignment requires you to return either a list of the prime numbers you found, or else a list indicating for each value in the range whether it is prime or not.

Sign in to comment.

Add to your function:
Prime=Prime(Prime>0);
(insert that in your function end)
edit primes %execute this for a surprise ;)

3 Comments

that depends, if you got one end or not for the function, if you have then insert the code before it, if not insert the code after your own code
Ummm, I don't think so, Paulo. "Prime" is going to have a scalar value at that point.

Sign in to comment.

function Prime = PrimeNum (N,M)
if (N>M || N <0 || M < 0)
error('ERROR: Invalid Input, you entered biggere number first or you entered negative number as you interval.');
end
Prime = [];
for j=0:(M-N)
for i=2:((N+j)/2)
if mod((N+j),i)~=0
Prime=[Prime;N+j];
end
end
end

3 Comments

its not working, its giving error in line " Prime=[Prime;N+j]; ".
abobroff, that code is going to repeat the same number many times in the list. For example, over 5 to 20, Prime would end up with the column vector which is the transpose of
5 7 7 8 9 9 10 10 11 11 11 11 12 13 13 13 13 13 14 14 14 14 15 15 15 15 16 16 16 16 17 17 17 17 17 17 17 18 18 18 18 19 19 19 19 19 19 19 19 20 20 20 20 20

Sign in to comment.

Excuse for my English. Haste without checking the work of construction:
for i = 2: ((N + j) / 2) if mod ((N + j), i) == 0 Prime = 0; end end
Further, the other variant:
function p = primenum2(m,n)
p = []; for j = m:n, if j-nnz(rem(j,1:j)) == 2, p = [p;j]; end;end

1 Comment

for i = 2: ((N + j) / 2)
if mod ((N + j), i) == 0
Prime = 0;
end
end
could be optimized to
for i = 2: ((N + j) / 2)
if mod ((N + j), i) == 0
Prime = 0;
break
end
end
Also, there is no reason to go as far as (N+j)/2, only as far as sqrt(N+j)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!