1.试除法:
#include<stdio.h>
int main()
{
int num = 0 ;
for (num = 100; num <= 200; num++)
{
int test = 2;
while (test <= num)
{
if(num%test==0)
{
printf("%d不是素数\n", num);
break;
}
test++;
if (test == num)
{
printf("%d是素数\n", num);
}
}
}
return 0;
}
2.试除法优化:
#include<stdio.h>
#include<math.h>
int main()
{
int i = 0;
int count = 0;
for (i = 100; i <= 200; i++)
{
int j = 0;
for (j = 2; j <= sqrt(i); j++)
{
if (i % j == 0)
{
break;
}
}
if (j > sqrt(i))
{
count++;
printf("%d\n", i);
}
}
printf("\ncount=%d\n",count);
return 0;
}