关于打印一事实上范围内的素数,请高手指正
#include <stdio.h>/*
功能:找出一定范围内的全部素数
*/
int main(void)
{
int i;
int j;
int k;
int m; //定义整形变量m用于存放需要的范围上限
printf("please type in a number,then we can find the prime numbers between 1 and the number you want\n");
scanf("%d",&m);
if (m >1)
{
printf("the prime numbers between 1 and %d are :\n",m);
for (i = 2; i <= m; i++)
{
if (i ==2)
{
printf("%d ",i);
}
for(j = 2; j < i; j++)
{
k=i%j;
if (k == 0)
break; //当i能被1到i本身中的数整除时,跳出当前循环,继续执行i++
else if (j == i-1) //直到i除以本身减1都无法整除时,确定此时i为素数
printf("%d ",i);
}
}
}
else //当输入不是大于1的自然数情况下的处理
printf ("please type in a right natural number");
printf("\n");
return 0;
}
/*
在VC++6.0中输出结果如下:
—————————————————————————————————————————
—————————————————————————————————————————
please type in a number,then we can find the prime numbers between 1 and the num
ber you want
300
the prime number between 1 and 300 are :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 1
07 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 2
23 227 229 233 239 241 251 257 263 269 271 277 281 283 293
Press any key to continue
*/
看了谭老师教材上求素数的方法,没太看懂,自己琢磨了一天才有点成果、