【1409】判决素数个数

【问题描述】
       输入两个整数X和Y,输出两者之间的素数个数(包括X和Y)。
【输入格式】:
       两个整数X和Y(1 <= X,Y <= 105)。
【输出格式】:
       输出一个整数,表示X,Y之间的素数个数(包括X和Y)。
【输入样例】:
        1 100
【输出样例】:
        25
【参考程序】

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;

bool prime(int x) {
	if (x <= 1) {
		return false;
	}
	int i = 2;
	while ((i<=sqrt(x)) && (x%i!=0)) {
		i++;
	} 
	
	return i>sqrt(x);
}

int main() {
	int x, y, cnt=0;
	cin >> x >> y;
	if (x > y) {
		swap(x, y);
	}
	
	for (int i=x; i<=y; i++) {
		if (prime(i)) {
			cnt++;
		}
	}
	cout << cnt;
	
	return 0;
}

发布了49 篇原创文章 · 获赞 0 · 访问量 1035

猜你喜欢

转载自blog.csdn.net/developer_zhb/article/details/104949519
今日推荐