View Code of Problem 103

#include<stdio.h>//门牌的风俗
#include<math.h>
int su(int a)//求素数,由主函数调用
{
	int i,b,c,d,e;
	if(a==2||a==3)
		c=1;
	else if(a==1)
		c=0;
	else
	{
		b=sqrt(a);
		e=0;
		for(i=2;i<=b;i++)
		{	
			d=a%i;
			if(d==0)
			{
				c=0;
				break;
			}
			else
				e=e+1;
		}
		if(e==b-1)
			c=1;
	}
	return(c);
}
main()
{
	int a,b,c,d,i;
	while(scanf("%d %d",&a,&b)!=EOF)//输入
	{
		d=0;
		for(i=a;i<=b;i++)
		{
			c=su(i);//判断是不是素数
			if(c==1)
				d=d+1;//如果是素数,加一
		}
		printf("%d\n",d);//输出最后结果值
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 103