- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
#include<stdio.h>
int main(){
int n,k=1;
int i,j;
unsigned long long int sum=0;
scanf("%d",&n);
for(i=2;i<n;i++){
for(j=2;j<=i;j++){
if(i%j==0)
k++;
}
if(k==2){
sum=sum+i;
}
k=1;
}
printf("%lld",sum);
return 0;
}
此代码对于输入 1000,10000
工作正常,但对于 100000,1000000
则不行,我的意思是它什么都不打印。我也试过 %I64d
但没有用。
如何打印 sum
?
最佳答案
我建议您在每次迭代时打印总和,而不仅仅是在结束时打印,这样您就可以看到计算的进度。除了更容易调试你的 printf
语句,然后您还可以检测到可能的回绕(例如,如果 unsigned long long 不足以求和)。
无论如何 -- 我认为您的问题实际上与您的 printf
有关格式字符串:使用 %llu
而不是 %lld
.请看下面的代码:
int main(){
int n,k=1;
int i,j;
unsigned long long int sum=0;
scanf("%d",&n);
for(i=2;i<n;i++){
for(j=2;j<=i;j++){
if(i%j==0)
k++;
}
if(k==2){
sum=sum+i;
}
k=1;
/* This helps you see what's going on: */
printf("sum is %llu\n",sum);
}
printf("%llu",sum);
return 0;
}
我用过%llu
而不是 %lld
,它工作正常。如果我把它改回 %lld
, 每次迭代都不会打印任何内容。
并确保您的输入 ( n
) 不能超过 int
的最大大小在你的平台上。 (但总和可能更大,因为您将其声明为 unsigned long long
)。
此外,在检查质数时,您可以进行一些基本的优化:
n/k<k
(或者如果 k*k>n
),您可以停止测试。此外,您可以使用 C 中可用的标准 bool 类型。
请参阅下面代码中的注释。
#include<stdio.h>
#include<math.h>
/* stdbool defines the type "bool", which can be "true" or "false": */
#include<stdbool.h>
int main(){
int n;
int i,j;
/* prime is initialized to true, since we presume every number is prime
until we find a divisor for it */
bool prime = true;
/* sum is initialized to zero. */
unsigned long long int sum=0;
scanf("%d",&n);
/* If n=2, the result is zero. But if it's greater than two, we need
to count 2 and start searching from 3. It's easier this way, because then
we can use i=i+2 in the for loop. */
if (n>2)
sum = 2;
/* no need to count from 2. We start from 3 and count every two numbers. */
for(i=3;i<n;i=i+2){
/* same here. And, since this is the divisor we're looking for, we only
need to go up to sqrt(i). We use the long casting here because checking
j*j<=i would not fit in an int type if j^2 is greater than the maximum
int. This is a corner case, that happens for example when i is maxint
(j-1)^2 is below and and j^2 is above it. */
for(j=3;(long)j*j<=i;j=j+2)
if(i%j==0) {
prime = false;
/* get out, since we already know that this one is composite! */
break;
}
/* no divisors? add it to sum! */
if (prime)
sum = sum + i;
/* reset prime to true, we'll start again with next i */
prime = true;
printf("sum is %llu\n",sum);
}
printf("%llu\n",sum);
return 0;
}
这些是您可以执行的相当简单的优化。还有其他的,有兴趣的可以研究下sieve of Erathostenes或者(如果你喜欢数学),Miller-Rabin test (这不是确定性的,但可能很有趣)。 C 库 GMP如果您想使用它,可以使用 Miller-Rabin。
关于c - 打印 64 位素数和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25789307/
我正在尝试制作一个素数列表。我已经对它进行了编码,但它只告诉我 1 - 100 的质数是 1。我不确定为什么会发生这种情况。我还想为它制作一个JFrame。 import javax.swing.JO
我正在尝试学习 lisp,但我在处理素数方面遇到了一些困难。我需要一个函数 is-prime,如果它是素数,我必须返回 t,如果不是,我必须返回 nil。 (prime 41) => t (prime
我正在尝试检查给定的数字是否是质数。首先采用试分割法。但该程序的行为很奇怪。这是我使用的。 int no; no = Integer.parseInt(jTextField1.getText());
判断一个数是否为素数。使用代码块 13.12 执行。编译器GNU GCC 4.7.0 #include main() { int input,check,count; printf(
从 Uncle Bob 的书 Clean Code(示例是用 Java 编写的,所以这是我的第一个 Java 翻译),我一直在研究素数的重构示例。 问题是:您将如何重构以下代码? 这里有 4 个版本:
此方法旨在递归地遍历该方法并判断数字是否为“ super 素数”。 super 素数是一个本身是素数的数字,每次它被 10 除时,所有这些数字也是素数。例如 2333 是超素数,因为 233 是素数,
这是一个接受输入并打印它是否为质数的代码最后一个 if 总是打印“false”,为什么? (print "Enter a positive integer") (setq num (read)) (s
我正在尝试生成一个 BigInteger 类型的随机质数,它介于我提供的最小值和最大值之间。 我知道 BigInteger.probablePrime(int bitlength, random),但
背景信息: 所以我想计算一个数内的所有质数,然后将它们相加。 sumPrimes(10) 应该返回 17 因为 2 + 3 + 5 + 7 = 17。 挑战来了 --> https://www.fre
我是 python 的初级程序员,对我正在编写的代码有疑问: number = int(input("Enter a random number: ")) for num in range(1, nu
质数又称素数。指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除的数。素数在数论中有着很重要的地位。比1大但不是素数的数称为合数。1和0既非素数也非合数。质数是与合数相对立的两个概
复制代码 代码如下: <?php for($i = 2; $i < 1001; $i++) { $primes = 0; for($k = 1; $k <
ruby判断一个数是否为质数 质数又称素数。一个大于1的自然数,如果除了1和它自身外,不能被其他自然数整除的数;(除0以外)否则称为合数 。根据算术基本定理,每一个比1大的整数,要么本身是一个质数
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
经过几个小时的谷歌搜索,我仍然处于停滞状态。如果有人指出我的公式或编码选择中的错误,我将不胜感激。请记住我是 Swift 的新手。我不习惯非 C 风格的 for 循环。 if textFiel
我已将我的问题上传为屏幕截图。 最佳答案 费马小定理 x^p mod p = x mod p or x^(p-1) mod p = 1 (if p does not divide x
初等数论 素数定义 设整数 \(p\ne 0,\pm 1\) 。如果 \(p\) 除了平凡约数以外没有其他约数,那么称 \(p\) 为素
我有以下数字数组: var array = [5,9,12,19,23,24,31,44,49,62]; 我试图找出每一个是偶数和合数,奇数和合数,还是奇数和素数。我首先尝试找到偶数和合数: for
import java.util.Scanner; public class array1 { public static void main(String [] args){
我是一名优秀的程序员,十分优秀!