Prime number or its nearest prime numbers

3 views (last 30 days)
anyone please tell me in matlab how to write a function to find prime number of any number if it is not prime number then function show its nearest prime numbers... let take example ...take number 9 we know it is not prime then the program will show its nearest prime like 7 and 11..
  2 Comments
Thomas
Thomas on 13 Apr 2012
if this is homework, can you show, what you have done so far?
Jan
Jan on 13 Apr 2012
What have you tried so far and which problem occurred?

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 13 Apr 2012
input x
x = 24;
solution
X = [x x];
out = zeros(1,2);
while 1
t = isprime(X);
if all(t)
out = X;
break
elseif any(t)
out(t) = X(t);
t1 = ~t;
X(t1) = X(t1) - sum([1 -1].*t1);
else
X = X - [1 -1];
end
end
out = unique(X)
  2 Comments
Jan
Jan on 13 Apr 2012
I assume, this is the solution of Rahul's homework, Andrei.
Rahul Singh
Rahul Singh on 14 Apr 2012
thanks Andrei Bobrov........i think this is useful answer for me............

Sign in to comment.

More Answers (1)

Sean de Wolski
Sean de Wolski on 13 Apr 2012
isprime() will be your friend.

Categories

Find more on Discrete Math 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!