Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. >>> solution(10) 2640 >>> solution(15) 13160 >>> solution(20) 41230 >>> solution(50) 1582700
(n: int = 100)
| 18 | |
| 19 | |
| 20 | def solution(n: int = 100) -> int: |
| 21 | """ |
| 22 | Returns the difference between the sum of the squares of the first n |
| 23 | natural numbers and the square of the sum. |
| 24 | |
| 25 | >>> solution(10) |
| 26 | 2640 |
| 27 | >>> solution(15) |
| 28 | 13160 |
| 29 | >>> solution(20) |
| 30 | 41230 |
| 31 | >>> solution(50) |
| 32 | 1582700 |
| 33 | """ |
| 34 | |
| 35 | sum_cubes = (n * (n + 1) // 2) ** 2 |
| 36 | sum_squares = n * (n + 1) * (2 * n + 1) // 6 |
| 37 | return sum_cubes - sum_squares |
| 38 | |
| 39 | |
| 40 | if __name__ == "__main__": |