mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4mobile wallpaper 5mobile wallpaper 6mobile wallpaper 7
29 字
1 分钟
素数求和
2026-05-28

题目描述#

给定两个整数n和m,求[n, m]范围内所有素数的和。

代码#

#include<stdio.h>
#include<math.h>
int main()
{
int n, m;
scanf("%d %d", &n, &m);
int i, j;
long long sum = 0;
for (i = n;i <= m;i++)
{
int x = 0;
for (j = 2; j <= sqrt(i); j++) {
if (i % j == 0)
{
x = 1;
}
}
if (x == 0)
{
sum += i;
}
}
printf("sum=%lld", sum);
}
分享

如果这篇文章对你有帮助,欢迎分享给更多人!

素数求和
https://blog.sherry.qzz.io/posts/c/素数/
作者
Sherry
发布于
2026-05-28
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录