count the quantity of prime number before my number

5 views (last 30 days)
Hi, I want to write a code to determine the quantity of prime number before the number selected by the user.
and I want to determine which numbers are prime inside of the loop. can you please help me?
x=input('Input a positive integer, or 0 to quit: ');
a=mod(x,1);
k=x-1;
for c=2:1:k
count=0;
for b=1:1:c
if mod(c,b)==0
count= count+1;
end
end
p=0;
if count==2
p=p+1; % c is prime
end
end
fprintf('count of prime numbers up to %g is: %g ',x,p);
end

Answers (1)

Andrei Bobrov
Andrei Bobrov on 20 Mar 2019
x=input('Input a positive integer, or 0 to quit: ');
z = [2,3:2:x];
y= mod(z(:),z(:)');
out = z(all(tril(y) + triu(ones(size(y))),2));

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!