您的位置:首页 > 其它

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

2017-12-11 22:56 621 查看
打印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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: