简体   繁体   English

前n个自然数之和

[英]Sum of first n natural numbers

Trying to learn C I'm toying around a bit with some for loops and sums.尝试学习 C 我正在玩一些 for 循环和求和。 I want to compute the sum of the first n natural numbers without using the mathematical formula n(n+1)/2 .我想在不使用数学公式n(n+1)/2情况下计算前n自然数的总和。 I have this code for it:我有这个代码:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n = 100;
    int sum = 0;

    for (int ix = 0; ix <= n; ix++) {
        sum = sum + ix;
    }

    printf("Sum of the first %d natural numbers is %d\n", n, sum);
}

So for n = 100 I get the sum to be 5050 , which is correct.所以对于n = 100我得到的总和为5050 ,这是正确的。 I also get correct when I use n = 10000 , however if I go for example n = 1000000 then I get the sum = 1784293664 but correct answer should be sum = 500000500000 .当我使用n = 10000 ,我也会得到正确的结果,但是如果我去例如n = 1000000那么我得到sum = 1784293664但正确答案应该是sum = 500000500000

Why does my program stop working when n becomes larger and what is that number of the sum being displayed when n = 1000000 ?为什么当n变大时我的程序停止工作,当n = 1000000时显示的总和数是多少?

If you want to calculate a sum of natural numbers then instead of the type int use the type unsigned int .如果要计算自然数的总和,则使用unsigned int类型代替 int 类型。

Correspondingly declare the variable sum as having the type unsigned long long int to decrease the risk of overflow.相应地将变量sum声明为unsigned long long int类型以减少溢出的风险。

For example例如

unsigned int n = 100;
unsigned long long int sum = 0;

for ( unsigned int ix = 1; ix <= n; ix++){
   sum = sum + ix;
}

printf("Sum of the first %u natural numbers is %llu\n" , n, sum);

Or you could include the header <inttypes.h> and use the type uintmax_t for the variable sum as it is shown in the demonstrative program below.或者您可以包含头文件<inttypes.h>并使用类型uintmax_t作为变量sum如下面的演示程序所示。

#include <stdio.h>
#include <inttypes.h>

int main(void) 
{
    unsigned int n = 1000000;
    uintmax_t sum = 0;

    for ( unsigned int ix = 1; ix <= n; ix++){
       sum = sum + ix;
    }

    printf("Sum of the first %u natural numbers is %" PRIuMAX "\n" , n, sum);
    
    return 0;
}

The program output is程序输出是

Sum of the first 1000000 natural numbers is 500000500000

Pay attention to that there is no need to introduce the auxiliary variable ix .注意不需要引入辅助变量ix The loop can look simpler as for example例如,循环看起来更简单

#include <stdio.h>
#include <inttypes.h>

int main(void) 
{
    unsigned int n = 1000000;
    uintmax_t sum = 0;

    while ( n ) sum += n--;
    
    printf( "Sum of the first %u natural numbers is %" PRIuMAX "\n" , n, sum );
    
    return 0;
}

int type is too small to contain numbers so huge, so an arithmetic overflow happens on the way up there. int类型太小,无法包含如此巨大的数字,因此在那里发生算术溢出。 What you observe is called undefined behaviour (UB for short), this is what officially happens in C when signed integers overflow (unsigned ones simply rollover to zero and on).您观察到的行为称为未定义行为(简称 UB),这是有符号整数溢出时在 C 中正式发生的情况(无符号整数简单地滚动到零并继续)。

You are hitting variable type 'int' limit.您正在达到变量类型“int”的限制。 Try using long data type to store the sum.尝试使用长数据类型来存储总和。

#include<stdio.h> int main(){ #include<stdio.h> int main(){

int i=0, sum=0, n=10; int i=0, sum=0, n=10;

while(i<=n){
    printf("%d\n",i);
   sum=sum+i;
i++;
}
printf("%d",sum);

return 0;返回0; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM