-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy path求给定范围内的所有素数.c
44 lines (39 loc) · 933 Bytes
/
求给定范围内的所有素数.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//求给定范围start〜end之间的所有素数。
#include<stdio.h>
#include<math.h>
int main()
{
int start, end, i, k, m, flag=1, h=0;
do
{
printf("Input START and END:");
scanf("%d%d", &start, &end);
}while(!(start>0 && start<end));
printf("......... prime table(%d-%d).........\n", start, end);
for(m=start; m<=end; m++)
{
k=sqrt(m);
for(i=2; i<=k; i++)
if(m%i==0)
{
flag=0;
break;
}
if(flag)
{
printf("%-4d",m);
h++;
if(h%10==0)
printf("\n");
}
flag=1;
}
printf("\nThe total is %d", h);
return 0;
}
//运行结果:
//Input START and END:1 100
//......... prime table(1-100).........
//1 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