简体   繁体   English

所有数字的总和

[英]Sum of all numbers

I need to write a function that calculates the sum of all numbers n. 我需要编写一个函数来计算所有数字n的总和。

Row 1:          1 
Row 2:         2 3 
Row 3:        4 5 6 
Row 4:       7 8 9 10 
Row 5:     11 12 13 14 15 
Row 6:   16 17 18 19 20 21 

It helps to imagine the above rows as a 'number triangle.' 将上述行想象为“数字三角形”有助于此。 The function should take a number, n, which denotes how many numbers as well as which row to use. 该函数应采用数字n,表示要使用的数字和数量。 Row 5's sum is 65. How would I get my function to do this computation for any n-value? 第5行的总和是65.如何让我的函数对任何n值进行计算?

For clarity's sake, this is not homework. 为清楚起见,这不是功课。 It was on a recent midterm and needless to say, I was stumped. 这是在最近的一个中期,不用说,我很难过。

The leftmost number in column 5 is 11 = (4+3+2+1)+1 which is sum(range(5))+1 . 第5列中最左边的数字是11 = (4+3+2+1)+1 ,它是sum(range(5))+1 This is generally true for any n . 这通常适用于任何n

So: 所以:

def triangle_sum(n):
    start = sum(range(n))+1
    return sum(range(start,start+n))

As noted by a bunch of people, you can express sum(range(n)) analytically as n*(n-1)//2 so this could be done even slightly more elegantly by: 正如一群人所指出的那样,你可以将sum(range(n))分析表示为n*(n-1)//2 ,这样可以通过以下方式更优雅地完成:

def triangle_sum(n):
    start = n*(n-1)//2+1
    return sum(range(start,start+n))

A solution that uses an equation, but its a bit of work to arrive at that equation. 一个使用方程的解决方案,但它有点工作来得出那个方程式。

def sumRow(n):
    return (n**3+n)/2

The numbers 1, 3, 6, 10, etc. are called triangle numbers and have a definite progression. 数字1,3,6,10等被称为三角形数字并且具有明确的进展。 Simply calculate the two bounding triangle numbers, use range() to get the numbers in the appropriate row from both triangle numbers, and sum() them. 只需计算两个边界三角形数字,使用range()从两个三角形数字中获取相应行中的数字,并使用sum()它们。

def sum_row(n):
    final = n*(n+1)/2
    start = final - n
    return final*(final+1)/2 - start*(start+1)/2

or maybe 或者可能

def sum_row(n):
    final = n*(n+1)/2
    return sum((final - i) for i in range(n))

How does it work: 它是如何工作的:

The first thing that the function does is to calculate the last number in each row. 该函数的第一件事是计算每行中的最后一个数字。 For n = 5, it returns 15. Why does it work? 对于n = 5,它返回15.为什么它有效? Because each row you increment the number on the right by the number of the row; 因为每行增加右边的数字除了行数; at first you have 1; 起初你有1个; then 1+2 = 3; 然后1 + 2 = 3; then 3+3=6; 然后3 + 3 = 6; then 6+4=10, ecc. 然后6 + 4 = 10,ecc。 This impy that you are simply computing 1 + 2 + 3 + .. + n, which is equal to n(n+1)/2 for a famous formula. 这意味着您只需计算1 + 2 + 3 + .. + n,对于着名的公式,它等于n(n + 1)/ 2。

then you can sum the numbers from final to final - n + 1 (a simple for loop will work, or maybe fancy stuff like list comprehension) Or sum all the numbers from 1 to final and then subtract the sum of the numbers from 1 to final - n, like I did in the formula shown; 那么你可以将数字从最终数字加到最终数字 - n + 1(一个简单的for循环可以工作,或者可能是像列表理解这样的花哨的东西)或者将所有数字从1加到final,然后从1减去数字的总和final - n,就像我在显示的公式中所做的那样; you can do better with some mathematical operations 你可以通过一些数学运算做得更好

Here is a generic solution: 这是一个通用的解决方案:

start=1
n=5
for i in range(n):
    start += len (range(i))
answer=sum(range(start,start+n))

As a function: 作为一个功能:

def trio(n):
    start=1
    for i in range(n):
            start += len (range(i))
    answer=sum(range(start,start+n))
    return answer
def compute(n):
   first = n * (n - 1) / 2 + 1
   last = first + n - 1
   return sum(xrange(first, last + 1))

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

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