#include<stdio.h>
#include<stdbool.h>
#include<math.h>
//【判断素数】从2除以到x本身
bool Is_prime(int number)
{
bool key=true;
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
key = false;
}
}
return key;
}
//【判断素数】从2除以到sqrt(x)
bool Is_prime1(int number)
{
bool key = true;
for (int i = 2; i < sqrt(number); i++)
{
if (number % i == 0)
{
key = false;
}
}
return key;
}
void Sum_Printf_Is_primenumber(int start, int end)
{
printf("从%d到%d的素数有:\n", start, end);
int sum = 0;
int start_s = start;
while (start_s <= end)
{
//if (Is_prime(start_s))
if (Is_prime(start_s))
{
printf("%d\n", start_s);
sum++;
}
start_s++;
}
printf("从%d到%d,总计有%d个素数。\n", start, end, sum);
printf("合数有%d个\n", end-start-sum+1);
}
int main()
{
int start;
int end;
printf("输入开始数值\n");
scanf_s("%d", &start);
printf("输入结束数值\n");
scanf_s("%d", &end);
Sum_Printf_Is_primenumber(start, end);
return 0;
}
2021-09-15【找素数】【bool】【sqrt】
最新推荐文章于 2024-08-28 09:50:23 发布