## Sequence summation in Python

DaLongggggg 2021-04-06 20:05:51
sequence summation python

Problem description 1+2+3+...+n Value . Input format input includes an integer n. Output format output one line , Include an integer , Express 1+2+3+...+n Value . The sample input 4 Sample output 10 The sample input 100

explain ： There are some test questions that give multiple sets of sample input and output to help you do better .

Generally, all these samples need to pass the test before they are submitted , But it doesn't mean that these groups of sample data are correct, and your program is completely correct , Potential mistakes may still lead to lower scores . Sample output 5050 Data scale and agreement 1 <= n <= 1,000,000,000.

explain ： Notice the size of the data here .

The direct idea of this problem is to directly use a cycle to accumulate , However , When the data scale is large , such “ violence ” This method often results in timeouts . At this point you need to think about other ways . You can try , If you use 1000000000 As input to your program , Can your program run within the time limit specified above .

Another point worth noting is that the size of the answer is not in the default integer of your language (int) Within the scope of , If you use integers to save results , It can lead to wrong results .

If you use C++ or C Language and ready to use printf Output results , Then your format string should be written as %I64d To output long long Type integer .

`` n = int(input())if (n>=1 and n<=1000000000): sum = int((n*(n+1))/2)print(sum)``

Sum of equal difference sequence https://pythonmana.com/2021/04/20210406195818629T.html