打印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;
}
第一种:
#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;
}
相关文章推荐
- 简单的算法题,包括1.打印100——200之间的的素数2.输出乘法口诀表3.判断1000年——2000年之间的闰年,给出完整代码
- 打印100~200之间素数的优化算法
- iOS 算法面试题(打印2-100之间的素数、求两个整数的最大公约数、查找字符串的位置)~demo
- 打印100-200之间的素数 C语言
- 打印100~200 之间的素数
- 打印100~200 之间的素数
- 打印100 - 200之间的素数
- c语言打印100~200之间的素数
- C语言求解素数(质数)的优化:打印100-200之间的素数
- 打印1~100之间的全体素数
- 打印某个区间之间的素数,并输出素数的个数。如100-200
- 打印100—200之间的素数
- 1.打印100~200之间的素数/2.判断1000年-2000年之间的闰年
- 用c语言打印100-200之间的素数
- 打印100~200 之间的素数,输出乘法口诀表,判断1000年---2000年之间的闰年
- Java中打印100-200之间的素数
- 用C打印100到200之间的素数
- 第4章, 4.14.2打印1~100之间的素数
- 整数算法训练04—求100以内的素数,全部打印出来
- [pascal]对“求1-100之间的所有素数”的三种不同循环结构算法的分析