打印100到400之间的素数的4种算法

打印100到200之间的素数。

第一种:

#include<stdio.h>
int main()
{
int i = 0;
int count = 0;
for (i = 100; i <= 200; i++)
{
int j = 0;
for (j = 2; j < i; j++)
{
if (i%j == 0)
break;
}
if (i == j)
{
printf("%d ", i);
count++;
}
}
printf("\ncount=%d\n", count);
system("pause");
return 0;
}

第二种:
#include<stdio.h>
int main()
{
int i = 0;
int j = 0;
int count = 0;
for (i = 100; i <= 200; i++)
{
for (j = 2; j <= i / 2; j++)
{
if (i%j == 0)
break;
}
if (j > i / 2)
{
printf("%d ", i);
count++;
}
}
printf("\ncount=%d\n", count);
system("pause");
return 0;
}


第三种:
#include<stdio.h>
#include<math.h>
int main()
{
int i = 0;
int j = 0;
int count = 0;
for (i = 100; i <= 200; i++)
{
for (j = 2; j <= sqrt(i); j++)
{
if (i%j == 0)
break;
}
if (j > sqrt(i))
{
printf("%d ", i);
count++;
}
}
printf("\ncount=%d\n", count);
system("pause");
return 0;
}



第四种:
#include<stdio.h>
#include<math.h>
int main()
{
int i = 0;
int count = 0;
for (i = 101; i <= 200; i += 2)
{
//判断i是否为素数
int j = 0;
for (j = 2; j <= sqrt(i); j++)
{
if (i%j == 0)
break;
}
if (j > sqrt(i))
{
printf("%d ", i);
count++;
}
}
printf("\ncount = %d\n", count);
system("pause");
return 0;
}





猜你喜欢

转载自blog.csdn.net/jgm20475/article/details/78777750