查找两个数字之间的素数(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 ...
-
最大执行时间错误处理(max execution time error handling)[2022-02-24]
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 ...
-
Perf Stat与Perf记录(Perf Stat vs Perf Record)[2023-09-30]
首先,您使用sleep和page-faults的测试用例不是一个理想的测试用例。 在睡眠期间不应该有任何页面错误事件,你真的不能期待任何有趣的事情。 为了便于推理,我建议使用ref-cycles (硬件)事件和繁忙的工作负载,例如awk 'BEGIN { while(1){} }' 。 问题1:据我所知,perf stat获取计数的“摘要”,但与-I选项一起使用时,会以指定的毫秒间隔获取计数。 使用此选项是否可以总结间隔内的计数或获取间隔内的平均值,或完全不同的其他内容? 我认为它总结了。 是。 这些值只是 ... -
microtime(true) - 无法测量PHP脚本执行时间(microtime(true) - Trouble measuring PHP script execution time)[2023-10-10]
根据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脚本执行时间差异的原因是什么?(What is causing the variance in the execution time of this python script?)[2022-07-12]
可能有几个因素。 我现在可以想到两个: 初始字节编译。 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 ...