[英]Function to calculate the difference between sum of squares and square of sums
I am trying to Write a function called sum_square_difference
which takes a number n and returns the difference between the sum of the squares of the first n natural numbers and the square of their sum.我正在尝试编写一个名为
sum_square_difference
的函数,它接受一个数字 n 并返回前 n 个自然数的平方和与其总和的平方之间的差。
I think i know how to write a function that defines the sum of squares我想我知道如何编写一个定义平方和的函数
def sum_of_squares(numbers):
total = 0
for num in numbers:
total += (num ** 2)
return(total)
I have tried to implement a square of sums function:我试图实现一个平方和函数:
def square_sum(numbers):
total = 0
for each in range:
total = total + each
return total**2
I don't know how to combine functions to tell the difference and i don't know if my functions are correct.我不知道如何组合函数来区分,也不知道我的函数是否正确。
Any suggestions please?请问有什么建议吗? I am using Python 3.3
我正在使用 Python 3.3
Thank you.谢谢你。
The function can be written with pure math like this:该函数可以用纯数学编写,如下所示:
Translated into Python:翻译成Python:
def square_sum_difference(n):
return int((3*n**2 + 2*n) * (1 - n**2) / 12)
The formula is a simplification of two other formulas:该公式是另外两个公式的简化:
def square_sum_difference(n):
return int(n*(n+1)*(2*n+1)/6 - (n*(n+1)/2)**2)
n*(n+1)*(2*n+1)/6
is the formula described here , which returns the sum of the squares of the first n
natural numbers. n*(n+1)*(2*n+1)/6
是这里描述的公式,它返回前n
自然数的平方和。
(n*(n+1)/2))**2
uses the triangle number formula, which is the sum of the first n
natural numbers, and which is then squared. (n*(n+1)/2))**2
使用三角形数公式,即前n
自然数之和,然后求平方。
This can also be done with the built in sum
function.这也可以通过内置的
sum
函数来完成。 Here it is:这里是:
def sum_square_difference(n):
r = range(1, n+1) # first n natural numbers
return sum(i**2 for i in r) - sum(r)**2
The range(1, n+1)
produces an iterator of the first n
natural numbers. range(1, n+1)
生成前n
自然数的迭代器。
>>> list(range(1, 4+1))
[1, 2, 3, 4]
sum(i**2 for i in r)
returns the sum of the squares of the numbers in r, and sum(r)**2
returns the square of the sum of the numbers in r. sum(i**2 for i in r)
返回sum(i**2 for i in r)
数字的平方和, sum(r)**2
返回 r 中数字总和的平方。
# As beta says,
# 正如测试版所说,
# (sum(i))^2 - (sum(i^2)) is very easy to calculate :)
# (sum(i))^2 - (sum(i^2)) 很容易计算:)
# A = sum(i) = i*(i+1)/2
# A = sum(i) = i*(i+1)/2
# B = sum(i^2) = i*(i+1)*(2*i + 1)/6
# B = sum(i^2) = i*(i+1)*(2*i + 1)/6
# A^2 - B = i(i+1)(3(i^2) - i - 2) / 12
# A^2 - B = i(i+1)(3(i^2) - i - 2) / 12
# :)
# :)
# no loops... just a formula !**
# 没有循环...只是一个公式!**
This is a case where it pays to do the math beforehand.在这种情况下,事先进行数学计算是值得的。 You can derive
closed-form solutions for both the sum of the squares and the square of the sum.
您可以为平方和和和的平方推导出封闭形式的解。 Then the code is trivial (and O(1)).
然后代码是微不足道的(和 O(1))。
Need help with the two solutions?需要这两种解决方案的帮助?
def sum_square_difference(n):
r = range(1,n+1)
sum_of_squares = sum(map(lambda x: x*x, r))
square_sum = sum(r)**2
return sum_of_squares - square_sum
In Ruby language you can achieve this in this way在 Ruby 语言中,您可以通过这种方式实现这一点
def diff_btw_sum_of_squars_and_squar_of_sum(from=1,to=100) # use default values from 1..100.
((1..100).inject(:+)**2) -(1..100).map {|num| num ** 2}.inject(:+)
end
diff_btw_sum_of_squars_and_squar_of_sum #call for above method
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.