这篇和前面那个不同,是用自定义函数来写的
#pragma warning(disable:4996)标签:return,函数,自定义,int,panduansushu,素数,printf From: https://blog.51cto.com/u_15880518/5890411
#include<stdio.h>
#include<math.h>//sqrt的头文件//sqrt(X)-对括号里的数开平方
int panduansushu(int q)
{
int z = 0;
for (z = 2;z <= q;z++)//优化后:for (z = 2;z <= sqrt(q);z++)
{
if (q % z == 0)
return 0;
}
return 1;
}
int main()
{
int i = 0;
int m = 0;
int n = 0;
printf("请输入需要求素数的区间:");
scanf("%d%d", &m, &n);
for (i = m;i <=n;i++)
{
if (panduansushu(i) == 1)
{
printf("%d是素数\n", i);
}
}
return 0;
}