c++求求m~n中的素数的个数
时间: 2023-05-21 11:02:52 浏览: 212
c++实现寻找素数
您好,以下是求解 m~n 中素数个数的 C 代码:
```
#include <stdio.h>
int is_prime(int n) {
if (n <= 1) {
return 0;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int count_primes(int m, int n) {
int count = 0;
for (int i = m; i <= n; i++) {
if (is_prime(i)) {
count++;
}
}
return count;
}
int main() {
int m = 1, n = 100;
int count = count_primes(m, n);
printf("There are %d prime numbers between %d and %d.\n", count, m, n);
return 0;
}
```
如果您有其他问题,欢迎继续提问。
阅读全文