I need to write a function which prints out prime number between the two arguments.
5 views (last 30 days)
Show older comments
Faras Momin
on 10 Apr 2011
Commented: mohamed gamal
on 10 Apr 2016
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
Andrei Bobrov
on 11 Apr 2011
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
Walter Roberson
on 11 Apr 2011
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)])
More Answers (5)
Sean de Wolski
on 11 Apr 2011
functon the_primes = primenum(N,M);
ns = N:M
the_primes = ns(isprime(ns));
0 Comments
Walter Roberson
on 10 Apr 2011
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
Walter Roberson
on 11 Apr 2011
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.
Paulo Silva
on 10 Apr 2011
Add to your function:
Prime=Prime(Prime>0);
(insert that in your function end)
edit primes %execute this for a surprise ;)
3 Comments
Paulo Silva
on 10 Apr 2011
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
Walter Roberson
on 11 Apr 2011
Ummm, I don't think so, Paulo. "Prime" is going to have a scalar value at that point.
Andrei Bobrov
on 10 Apr 2011
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
Walter Roberson
on 11 Apr 2011
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
Andrei Bobrov
on 11 Apr 2011
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
Walter Roberson
on 11 Apr 2011
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)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!