Exercism: Difference Of Squares (Python)

求平方和和平方的和的差(很绕口)。很简单使用列表解析就可以。扫了一眼其他人的答案,有直接用公式的人。平方和我到是知道,不过平方的和我还真没记过。

平方和  ( n(n-1)/2 )** 2
平方的和  (n * (n + 1) * (2n + 1) / 6


def square_of_sum(number):
    return sum(i for i in range(1,number+1)) ** 2

def sum_of_squares(number):
    return sum(i ** 2 for i in range(1, number+1))

def difference_of_squares(number):
    return square_of_sum(number) - sum_of_squares(number) 

评论

  1. The company plans to proceed developing their providers till everybody on the planet can afford secure and affordable housing. With today’s new constructing supplies, houses may be constructed in 24 hours with 3D printing. The 3D printing industry is valued at greater than $17 billion and it is anticipated to greater than double that pvc water guns by 2026.

    回复删除

发表评论

此博客中的热门博文

Exercism: RNA Transcription (Python)

拜厄练习曲44, 45

拜厄练习曲38, 39