首页 \ 问答 \ 查找两个数字之间的素数(Finding Prime Numbers between Two Numbers)

查找两个数字之间的素数(Finding Prime Numbers between Two Numbers)

我试图找到两个给定数字之间的所有素数并将总数加起来。

我有这个循环,正确地检测素数检测。

但是,由于某种原因,我不知道如何总结所有的素数。

int a,b,i,j,sum=0;

do
{   cout << "Enter a number: ";
    cin >> a;
    if (a < 4 || a > 1000000) 
    {   cout << "Input must be between 4 and 1000000 inclusive." << endl;
    }
}while (a < 4 || a > 1000000);

do
{   cout << "Enter a second number: ";
    cin >> b;
    if (b < 4 || b > 1000000) 
    {   cout << "Input must be between 4 and 1000000 inclusive." << endl;
    }
}while (b < 4 || b > 1000000);

if (a > b)
{   int hold;
    hold = b;
    b = a;
    a = hold;
}

cout << "The prime numbers between " << a << " and " << b << " inclusive are: " << endl;
//int sum;
for (i = a; i <= b; i++)
{
 for (j = 2; j <= i; j++) // Changed the < to <=, and got rid of semicolon
 {
    if (!(i%j)&&(i!=j)) break;
    if (j==i) 
    {
              cout << i << endl;
              sum += i;
              cout << sum ;

    }
 }
}

可变sum给我垃圾结果。


Am trying to find all prime numbers between two given numbers and sum the primes up.

I have this loop that does the prime number detection correctly.

However, for some reason I don't know how to sum all the primes.

int a,b,i,j,sum=0;

do
{   cout << "Enter a number: ";
    cin >> a;
    if (a < 4 || a > 1000000) 
    {   cout << "Input must be between 4 and 1000000 inclusive." << endl;
    }
}while (a < 4 || a > 1000000);

do
{   cout << "Enter a second number: ";
    cin >> b;
    if (b < 4 || b > 1000000) 
    {   cout << "Input must be between 4 and 1000000 inclusive." << endl;
    }
}while (b < 4 || b > 1000000);

if (a > b)
{   int hold;
    hold = b;
    b = a;
    a = hold;
}

cout << "The prime numbers between " << a << " and " << b << " inclusive are: " << endl;
//int sum;
for (i = a; i <= b; i++)
{
 for (j = 2; j <= i; j++) // Changed the < to <=, and got rid of semicolon
 {
    if (!(i%j)&&(i!=j)) break;
    if (j==i) 
    {
              cout << i << endl;
              sum += i;
              cout << sum ;

    }
 }
}

The variable sum gives me rubbish results.


原文:https://stackoverflow.com/questions/10571855
更新时间:2021-12-08 10:12

最满意答案

使用--ns标志运行perf脚本可以解决问题。

perf script -F time --ns
76970.575503465:
76970.575506464:
76970.575507479:

无:

$perf script -F time
76970.575503:
76970.575506:
76970.575507:

从文档:

perf script -h
--ns              Use 9 decimal places when displaying time

Running perf script with --ns flag solves the issue.

perf script -F time --ns
76970.575503465:
76970.575506464:
76970.575507479:

Without:

$perf script -F time
76970.575503:
76970.575506:
76970.575507:

From documentation:

perf script -h
--ns              Use 9 decimal places when displaying time

相关问答

更多
  • 最简洁的解决方案是实现一个TaskExecutionListener (我相信你可以处理该部分)并用gradle.taskGraph.addTaskExecutionListener注册它。 The cleanest solution is to implement a TaskExecutionListener (I'm sure you can handle that part) and register it with gradle.taskGraph.addTaskExecutionListener ...
  • 您可以。 以下查询应返回每个可执行文件的持续时间。 DECLARE @DATE DATE = GETDATE() - 7 SELECT [executions].[folder_name] , [executions].[project_name] , [executions].[package_name] , [executable_statistics].[execution_path] , DATEDIFF(minute, [executable_s ...
  • function say_goodbye() { if (connection_aborted()) { // Perform some action if user has aborted the transaction } elseif (connection_status() == CONNECTION_TIMEOUT) { // perform some other action if the connection has timed out } els ...
  • 使用--ns标志运行perf脚本可以解决问题。 perf script -F time --ns 76970.575503465: 76970.575506464: 76970.575507479: 无: $perf script -F time 76970.575503: 76970.575506: 76970.575507: 从文档: perf script -h --ns Use 9 decimal places when displaying time Running ...
  • 它是默认的--fields列表( -F是等价的)输出,它看起来像可用字段列表中的pid , cpu和time 。 你可以验证使用 perf script -F pid,cpu,time 如果输出中的第一个字段相同。 如果没有,可能没有比从手册页尝试其他字段更好的方法(没有好的文档)。 It is output from the default list of --fields ( -F is equivalent ), it looks like pid, cpu and time from the li ...
  • 字符R/T/S/D代表各种任务状态。 字符'R'表示任务处于TASK_RUNNING状态。 字符“S”表示该任务已被置于TASK_INTERRUPTIBLE状态。 字符'D'表示调度程序已将该任务置于TASK_UNINTERRUPTIBLE状态。 最后,字符'T'表示任务当前处于TASK_STOPPED状态。 要了解任务状态如何可以从字符中确定,请查阅linux内核(4.17)的源代码: - TASK_STATE_TO_CHAR_STR宏 #define TASK_STATE_TO_CHAR_STR "RS ...
  • 首先,您使用sleep和page-faults的测试用例不是一个理想的测试用例。 在睡眠期间不应该有任何页面错误事件,你真的不能期待任何有趣的事情。 为了便于推理,我建议使用ref-cycles (硬件)事件和繁忙的工作负载,例如awk 'BEGIN { while(1){} }' 。 问题1:据我所知,perf stat获取计数的“摘要”,但与-I选项一起使用时,会以指定的毫秒间隔获取计数。 使用此选项是否可以总结间隔内的计数或获取间隔内的平均值,或完全不同的其他内容? 我认为它总结了。 是。 这些值只是 ...
  • 根据PHP文档,计算(参数get_as_float为true)将以秒为单位给出结果。 默认情况下,microtime()以“msec sec”的形式返回一个字符串,其中sec是自Unix纪元以来的秒数(格林威治标准时间1970年1月1日0:00:00),msec测量自秒以来已经过的微秒数并以秒表示。 如果get_as_float设置为TRUE,则microtime()返回一个浮点数,表示自Unix纪元精确到最接近的微秒以来的当前时间(以秒为单位)。 有关全文,请参阅http://php.net/manual ...
  • 可能有几个因素。 我现在可以想到两个: 初始字节编译。 Python将编译后的字节码缓存在.pyc文件中,首次运行时需要创建该文件,后续运行只需要验证字节码缓存上的时间戳。 磁盘缓存 Python解释器,你直接引用的3个库, 这些库使用的所有库,都需要从磁盘加载,除了脚本和它的字节码缓存之外。 操作系统会缓存此类文件以便更快地访问。 如果您在同一系统上运行其他内容,那些文件将从缓存中刷新,需要再次加载。 这同样适用于目录列表; 检查模块搜索路径中的模块的位置以及字节码缓存的测试都由缓存的目录信息加速。 如果 ...
  • 看起来你的数组实际上是由id索引的,所以这样做: foreach ($result1 as $key => $value) { if (isset($result[$key])) { $result[$key] += $value; } } 当然,如果可能的话,使用JOIN在数据库中完成所有这些工作最有意义。 Looks like your arrays are actually indexed by the ids, so do this: foreach ($resul ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。