1013 数素数 (20分)(C语言)

1013 数素数 (20分)

令 P
​i
​​ 表示第 i 个素数。现任给两个正整数 M≤N≤10
​4
​​ ,请输出 P
​M
​​ 到 P
​N
​​ 的所有素数。

输入格式:
输入在一行中给出 M 和 N,其间以空格分隔。

输出格式:
输出从 P
​M
​​ 到 P
​N
​​ 的所有素数,每 10 个数字占 1 行,其间以空格分隔,但行末不得有多余空格。

输入样例:

5 27

输出样例:

11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103
#include <stdio.h>
#include <math.h>
int main ()
{
    int m,n,x=0,b,sum=0,flog=1;
    int i,j;
	scanf("%d",&m);
	scanf("%d",&n);
    for(i=2;i<=104729;i++)
    {
		b=1;
   		for(j=2;j<=sqrt(i);j++)
   			 if(i%j==0)
			 {
				  b=0;
   			 break;
			 }
			 if(b==1)
			 {
                 if(sum%10==0&&sum!=0)
                {
                printf("\n");
                flog=1;
                }
                x++;
                if(x>=m&&x<=n)
                {
                    if(flog==1)
                    {
                    printf("%d",i);
                    flog=0;
                    }
                    else
                    printf(" %d",i);
                sum++;
                }
                
			 }
             if(x>n)
				 break;
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39915248/article/details/107967493