內容 :

你的好朋友質數先生又來找你囉,給你兩個數字,請算出這兩個數字包含的範圍內有幾個質數。

輸入說明 : 

輸入兩個正整數a,b(1<=a<=b<=100000000)。

 保證b-a<=1000 

輸出說明 : 

輸出一個非負整數,代表a到b之間(包含a,b)總共有幾個質數。
 
範例輸入 : 
3 7
6 6
30 50

範例輸出 :

3
0
5

/*
判斷範圍內的質數 
*/
#include<stdio.h>
#include<math.h>

int main()
{
	int n,m,i,j,flag,count;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		count=0;
		for(i=n;i<=m;i++)
		{
			flag=0;
			for(j=2;j<=sqrt(i);j++)
			{
				if(i%j==0)
				{
					flag=1;
					break; //i判斷為非質數,跳出迴圈 
				}
			}
			if(i==1);
			else if(!flag || i==2)
			{
				count++;
			}
		}
			printf("%d\n",count);
	}
	return 0;
}
 

arrow
arrow
    全站熱搜

    東勢厝滴yang 發表在 痞客邦 留言(0) 人氣()