/python/difference-of-squares/difference_of_squares.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def square_of_sum(n):
    """
    Square of sum 1..n

    Using the triangle number formula
    """
    return ((n * (n+1)) / 2)**2


def sum_of_squares(n):
    """
    Sum of all squares 1..n**2

    Using the sum of squares formula
    """
    return (n * (n+1) * (2*n + 1)) / 6


def difference(n):
    """
    Difference between the sum of squares 1..n and square of sums 1..n
    """
    return square_of_sum(n) - sum_of_squares(n)

@ffafara, @shxfee, @AlbericC, and @Karamba91 think this looks great

Comments


You're not logged in right now. Please login via GitHub to comment