count the quantity of prime number before my number

1 回表示 (過去 30 日間)
Melis Ozdemir
Melis Ozdemir 2019 年 3 月 20 日
回答済み: Andrei Bobrov 2019 年 3 月 20 日
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

回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2019 年 3 月 20 日
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));

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by