-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifference_of_squares.py
More file actions
51 lines (37 loc) · 1.13 KB
/
Copy pathdifference_of_squares.py
File metadata and controls
51 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Difference of Squares
"""
def square_of_sum(number):
"""
square of the sum
using the partial sum:
$sum_(n=1)^k n = 1/2 k (k + 1)$
https://www.wolframalpha.com/input?i=sum+n%2C+n%3D1+to+infinity
"""
return (number*(number+1)//2)**2
# # using generator expression
# return sum(index for index in range(number+1))**2
# # or simply
# return sum(range(number+1))**2
def sum_of_squares(number):
"""
sum and the sum of the squares
using the partial sum:
$sum_(n=1)^k n^2 = 1/6 k (k + 1) (2 k + 1)$
https://www.wolframalpha.com/input?i=sum+n%5E2%2C+n%3D1+to+infinity
"""
return number*(number+1)*(2*number+1)//6
# # using generator expression
# return sum(index**2 for index in range(number+1))
def difference_of_squares(number):
"""
The difference between the square of the sum and the sum of the squares
"""
# # simple solution:
# sums = 0
# squares = 0
# for index in range(number+1):
# sums += index
# squares += index**2
# return sums**2 - squares
return square_of_sum(number) - sum_of_squares(number)