C Euler 10,素数之和为2000000
我的算法有问题。我在下面的任务中找不到哪里出了问题 说明: 低于C Euler 10,素数之和为2000000,c,sum,primes,eulerr,C,Sum,Primes,Eulerr,我的算法有问题。我在下面的任务中找不到哪里出了问题 说明: 低于10的素数之和为2+3+5+7=17。 求200万以下所有素数之和 以下是我的C语言解决方案: #include <stdio.h> #include <stdbool.h> #include <stdlib.h> int main() { bool *numbers = (bool *)malloc(sizeof(bool) * 2000000); unsigned long
10
的素数之和为2+3+5+7=17
。
求200万以下所有素数之和
以下是我的C语言解决方案:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int main() {
bool *numbers = (bool *)malloc(sizeof(bool) * 2000000);
unsigned long run = 1;
while (run < 2000000) {
numbers[run] = true;
run++;
}
unsigned long sum = 0;
for (long i = 2; i < 2000000; i++) {
if (numbers[i] == true) {
for (long x = 2 * i; x < 2000000; x += i) {
numbers[x] = false;
}
}
}
run = 0;
while (run < 2000000) {
if (numbers[run] == true) {
sum = sum + run;
}
run++;
}
printf("%d\n", sum-1); // cause 1
free(numbers);
return 0;
}
#包括
#包括
#包括
int main(){
bool*数字=(bool*)malloc(sizeof(bool)*2000000);
无符号长期运行=1;
而(运行<2000000){
数字[运行]=真;
运行++;
}
无符号长和=0;
用于(长i=2;i<2000000;i++){
如果(数字[i]==真){
对于(长x=2*i;x<2000000;x+=i){
数字[x]=假;
}
}
}
run=0;
而(运行<2000000){
如果(数字[运行]==真){
总和=总和+运行;
}
运行++;
}
printf(“%d\n”,sum-1);//原因1
免费(数字);
返回0;
}
谢谢你的帮助 您可以尝试以下方法:
#include <stdio.h>
#include <stdlib.h>
int main(){
unsigned long int i,j;
unsigned long long sum=0; // the sum is potentially large
unsigned long cnt=0;
int limit=2000000; // the limit for the sums
char *primes; // only needs to be used as a flag holder
primes = malloc(sizeof(char)*limit); // flag of is prime or not
if(primes==NULL) {
perror("main, malloc");
return(-1);
} // else
for (i=2;i<limit;i++) // set the flags to True to start
primes[i]=1;
for (i=2;i<limit;i++) // now go through all combos
if (primes[i]) // already know; no need
for (j=i;i*j<limit;j++) // has i and j as factors
primes[i*j]=0; // not prime
for (i=2;i<limit;i++) // now add them up
if (primes[i]) {
sum+=i;
cnt++;
}
// report what was found; note %lu for long or %llu for long long
printf("There are %lu primes less than %d with a sum of %llu",
cnt, limit, sum);
return 0;
}
两种印刷品:
There are 148933 primes less than 2000000 with a sum of 142913828922
注:
long
。而long可以是32位或64位,int
可以短至16位李>
代码中的问题是,您没有为类型为无符号长的
sum
使用正确的printf
转换说明符:您应该编写:
printf("%lu\n", sum);
结果,142913828923
超出了32位整数的范围,因此实际上应该使用更大的类型,例如long
,它保证至少有63个值位。最后一个循环应该从2
开始,以避免将1
计数为素数
以下是经过一些改进的修改版本:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int main() {
int limit = 2000000;
bool *numbers = (bool *)malloc(sizeof(bool) * limit);
if (numbers == NULL) {
printf("not enough memory\n");
return 1;
}
for (int run = 0; run < limit; run++) {
numbers[run] = true;
}
for (long long i = 2; i * i < limit; i++) {
if (numbers[i] == true) {
for (long long x = i * i; x < limit; x += i) {
numbers[x] = false;
}
}
}
long long sum = 0;
for (int run = 2; run < limit; run++) {
if (numbers[run] == true) {
sum = sum + run;
}
}
printf("%lld\n", sum);
free(numbers);
return 0;
}
#包括
#包括
#包括
int main(){
整数限值=2000000;
bool*数字=(bool*)malloc(sizeof(bool)*限制);
如果(数字==NULL){
printf(“内存不足\n”);
返回1;
}
对于(int run=0;run
素数之和在我看来,我的LONG_MAX是正常的:9223372036854775807,但谢谢,我以前从未检查过这些值。有一个非常有效的算法等待10分钟,我会给你代码。帖子中没有问题。您不需要说明程序给出了什么输出,或者您希望得到什么输出。请阅读关于和关于提供a的内容。不要将数字标记为素数,而是使用calloc
分配零内存(false),并通过将数字设置为true
将数字标记为复合。然后数一数所有的谎言。更简单的代码。此外,您可以使用2初始化sum,并跳过每个偶数,因为它们无论如何都是复合的。最后,在启用优化的情况下进行编译。只有long-long才能保证在windowsCode上有足够的精度,没有解释不是一个好的答案代码>?阵列比需要的大得多。使用bool
或char
类型。@EricPostChil:fixed@chqrlie:很好的评论--已修复。
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int main() {
int limit = 2000000;
bool *numbers = (bool *)malloc(sizeof(bool) * limit);
if (numbers == NULL) {
printf("not enough memory\n");
return 1;
}
for (int run = 0; run < limit; run++) {
numbers[run] = true;
}
for (long long i = 2; i * i < limit; i++) {
if (numbers[i] == true) {
for (long long x = i * i; x < limit; x += i) {
numbers[x] = false;
}
}
}
long long sum = 0;
for (int run = 2; run < limit; run++) {
if (numbers[run] == true) {
sum = sum + run;
}
}
printf("%lld\n", sum);
free(numbers);
return 0;
}