计算低于200万的所有素数的总和. [英] Calculate the sum of all the primes below two million.

查看:93
本文介绍了计算低于200万的所有素数的总和.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算所有素数在2 mil以下的和,这是我的代码,但是结果无效,这是什么问题

Hi I want to calculate the sum of all primes bellow two milion and this is my code but the result is not valid what''s the problem

#include <iostream>
#include <math.h>

using namespace std;

int main() {

int sum=2;

int s;
for ( s=3;s<20;s+=2)
{
    bool is_prime = true;

    for (int i = 2; i <= sqrt(s); i++)
    {

        if (s % i == 0)
            is_prime = false;
    }
    if (is_prime)
    {

        sum=sum+s;

    }


}
cout <<sum;


return 0;
}

推荐答案

1.如果您不知道出了什么问题,请将您的问题分解为更小的问题!在这种情况下(a)验证您计算的素数确实是(所有)素数. (例如打印第一个结果)和(b)验证您的总和计算正确(例如打印前几个中间结果)

注意:比打印更好的方法是使用调试器并在前几次迭代中检查变量的值.这样一来,您不必修改代码,也不必冒意外破坏代码的风险.

2.对预期结果进行估计,或者尽可能获取实际值.然后将其与您的结果进行比较:是否相差很小?它更像是2或20或50的因数吗?还是完全关闭?

根据您所看到的情况,可能有完全不同的原因:
-在第一种情况下,它可能与初始化,循环的开始方式或循环的结束有关;
-在第二种情况下,我真的不能说,但是您应该在早期迭代中看到这种偏差,因此请回到步骤1并使用调试器;
-在最后一种情况下,您可能会遇到数字限制,例如. G.整数溢出:尝试使用longlong long而不是int求和,或者如果无效,请尝试double. (注意:您的编译器可能不支持long long)
1. If you don''t know what''s wrong, cut your problem into smaller ones! In this case (a) verify that the primes you calculate are indeed (all the) primes. (e. g. print the first ones) And (b) verify that your sum is calculated correctly (e. g. print the first few intermediate results)

Note: a better method than printing is to use the debugger and inspect the values of your variables throughout your first couple of iterations. That way you don''t have to modify your code and don''t risk accidentally breaking it.

2. Make an estimation of the expected result, or get the actual value if you can. Then compare it against your result: is it off by just a small difference? Is it more like a factor of 2, or 20, or 50? Or is it completely off?

Depending on what you see there may be quite different causes:
- in the first case it may be related to your initialization, how you start your loop, or how you end your loop;
- in the second case I really can''t say, but you should see that deviation in early iterations, so go back to step 1 and use a debugger;
- in the last case, you may have run into a numerical limitation, e. g. an integer overflow: try using long or long long instead of int for sum, or if that doesn''t work, try double. (note: your compiler may not support long long)


这并不简单,这是一个very简单任务.如果您不能自己完全解决问题,那么进一步的学习将毫无用处.您需要一些(有些许成功)才能开始信任您自己的功能.从这个非常简单的任务开始.向自己证明自己对某事有好处,并且可以自行解决至少一些问题,而无需任何帮助.对于初学者来说,这是相当不错的练习之一,它将使您感受到编程的工作方式.不要错过这次学习一些东西的机会.并且不要通过请别人帮助您来浪费这个宝贵的机会.不要小看它的价值.

一个-非常介绍性的建议-从一开始就学习如何一次迈出一步.听说过分离关注点吗?尽您所能,尽可能地分离问题,尤其是在开始时.在这种情况下,请创建单独的功能IsPrime(unsigned int). (使用unsigned intunsigned long,而不是int,学习选择适当的类型.)还有一个用于求和的单独函数.首先,关注点的分离,可读性和思想的明确表达应是您的首要任务.优化将在以后出现. (无论如何,该任务将很快执行.)

祝你好运,
—SA
This is not simple, this is a very simple task. If you cannot solve it fully by yourself, your further learning can be pretty much useless. You need something, some minor success, to start trusting your own capabilities. Start with this very simple task. Proof to yourself you are good for something, and can solve at least some problems without any help, all on your own. This is one of the pretty good exercises for the beginners, will let you feel how programming works. Don''t miss this chance to learn something. And do not screw up this valuable chance by asking someone to help you. Don''t underestimate the value of it.

One — very introductory — advice: from the very beginning, learn how to make one step at a time. Ever heard about separation of concerns? Try to separate concerns as much as you can, especially at first. In this case, make a separate function IsPrime(unsigned int). (Use unsigned int, or unsigned long, not int, learn to choose adequate types.) And a separate function for finding a sum. At first steps, separation of concerns, readability and explicit expression of the ideas should be your first priority. Optimization will come later. (And this task will execute very quickly anyway.)

Good luck,
—SA


这篇关于计算低于200万的所有素数的总和.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆