#include <iostream>

bool isPrime(int n) {
    if (n == 2) return true;
    if (n < 2 || n % 2 == 0) return false;
    for (int i = 3; i * i <= n; i += 2) {
        if (n % i == 0) return false;
    }    return true;
}

int countPrimes(int A, int B) {
    int count = 0;
    for (int i = A; i <= B; i++) {
        if (isPrime(i)) count++;
    }
    return count;
}

int main() {
    int A, B;
    std::cin >> A;
    std::cin >> B;
    std::cout << countPrimes(A, B) << std::endl;
    return 0;
}
C++ 代码实现素数计数 - 统计两个整数之间的素数数量

原文地址: https://www.cveoy.top/t/topic/qoNh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录