素数个数
【题目描述】
编程求2~n(n为大于2的正整数)中有多少个素数。
【输入描述】
输入n (2<= n <=50000)
【输出描述】
素数个数
【输入样例】
10
【输出样例】
4
#include<iostream>
#include<cmath>
using namespace std;
bool judge(int x);
int main()
{
int n;
int i;
int sum=0;
cin>>n;
for(i=2; i<=n; i++)
if(judge(i))//若是素数
sum++;//累加素数个数
cout<<sum<<endl;
return 0;
}
bool judge(int x)//判断素数
{
int i=2;
while(i<=floor(sqrt(x))&&(x%i)!=0)
i++;
if(i>floor(sqrt(x)))
return true;
return false;
}
.jztagtree{max-height:85vh;right:0px}.jzDown{top:10vh}.jztagtree li a{background-color:#448EF6}.jztagtree li a:before{border-right:10px solid #448EF6}.jztagtree li a:hover{background:#0045a6}.jztagtree li a:hover::before{border-right:10px solid #0045a6}
$("#jztoc").toc({content: ".single", headings: "h1,h2,h3"});